1

I have below dataframe

col1     col2
a        b
b        a
c        d
d        c
e        d

Desired Output should be unique pair from two columns

col1    col2
a        b
c        d
e        d
meTchaikovsky
  • 7,478
  • 2
  • 15
  • 34
Ganesh M
  • 548
  • 1
  • 4
  • 14

1 Answers1

1

Convert values to frozenset and then filter by DataFrame.duplicated in boolean indexing:

df = df[~df[['col1','col2']].apply(frozenset, axis=1).duplicated()]
print (df)
  col1 col2
0    a    b
2    c    d
4    e    d
    

Or you can sorting values by np.sort and remove duplicates by DataFrame.drop_duplicates:

df = pd.DataFrame(np.sort(df[['col1','col2']]), columns=['col1','col2']).drop_duplicates()
print (df)
  col1 col2
0    a    b
2    c    d
4    d    e
    
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252