0

I have the data below

   a   b
1  12  1
2  14  2
3  12  3
4  12  3
5  14  1
6  12  4

I have to group the data according to the column (a)..after which i have to delete the rows which has the same (b) value. Below i have shown what it should look like,

   a   b
1  12  1
2  12  4
3  14  1
4  14  2

is there any easy way in pandas to do this?

Anna Ssaa
  • 53
  • 1
  • 2
  • 6

1 Answers1

2

You can drop the duplicates with keep parameter specified as False:

import pandas as pd
df.drop_duplicates(keep=False)

#    a  b
#1  12  1
#2  14  2
#5  14  1
#6  12  4

If there are more columns in the data frame, columns can be explicitly specified as df.drop_duplicates(['a', 'b'], keep=False)

Psidom
  • 209,562
  • 33
  • 339
  • 356