S Ringne S Ringne - 21 days ago 5
Python Question

row wise sorting in pandas dataframe and remove duplicates

I have a table in pandas dataframe df

col1 col2 count
12 15 3
13 17 5
1 36 4
15 12 7
36 1 4


and so on....
what i want is to consider calues like '12 and 15' and '15 and 12' to be same, and also sum their count
and create a new dataframe df2
which has no repeated entries and their count to be added.
for eg.

col1 col2 count
12 15 10 #3+7
13 17 5
1 36 8 #4+4


and so on.
which method i should use to get that?
Thanks in advance

Answer

Sort your rows, then group and sum:

df[['a', 'b']] = df[['a', 'b']].apply(sorted, axis=1)
df.groupby(['a', 'b'], as_index=False)['c'].sum()