Given a dataframe
data = {
"col1": ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D'],
"col2": ['B', 'C', 'A', 'C', 'D', 'B', 'E', 'A',],
"count": [3, 7, 12, 2, 8, 2, 5, 9]
}
df = pd.DataFrame(data=data)
col1 col2 count
A B 3
A C 7
B A 12
B C 2
C D 8
C B 2
D E 5
D A 9
I want to aggregate all the rows that have symmetric pairs while maintaining the rows that don't. Therefore, the output would be a new dataframe
col1 col2 count
A B 15
A C 7
B C 4
C D 8
D E 5
D A 9
I have looked at Aggregate symmetric pairs pandas and Find symmetric pairs quickly in numpy but neither of them particularly help me.