6

I have dataset, the dataset have pairing duplication. Here's my data

Id    antecedent           descendant
1     one                  two
2     two                  one
3     two                  three
4     one                  three
5     three                two

Here's what I need, because one, two is equals two, one so I want ro remove the duplicate pair

Id    antecedent           descendant
1     one                  two
3     two                  three
4     one                  three
Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70

1 Answers1

7

Use numpy.sort for sort per rows with duplicated for boolean mask:

df1 = pd.DataFrame(np.sort(df[['antecedent','descendant']], axis=1))

Or:

#slowier solution
#df1 = df[['antecedent','descendant']].apply(frozenset, 1)

df = df[~df1.duplicated()]
print (df)
   Id antecedent descendant
0   1        one        two
2   3        two      three
3   4        one      three
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252