0

I would like to keep the unique combination of two columns. For example, A and B is the same as B and A.

df = pd.DataFrame({'col1': [1,2,4,3], 'col2': [2,1,3,4]})

       col1 col2
    0   1     2
    1   2     1
    2   4     3
    3   3     4

Desired outcome:

       col1  col2
     0  1   2
     1  4   3

Thanks alot :)

Shrief Nabil
  • 59
  • 1
  • 8

1 Answers1

0

You can do np.sort then drop_duplicates

df[:] = np.sort(df.values,1)
out = df.drop_duplicates()
Out[625]: 
   col1  col2
0     1     2
2     3     4
BENY
  • 317,841
  • 20
  • 164
  • 234