Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove groups where there is not at least two difference values within a column in pandas

I have a dataframe such as

COL1 COL2 SP
G1   A    SP1
G1   A    SP2
G2   B    SP1
G2   B    SP1
G3   C    SP7
G3   C    SP3
G4   A    SP8
G4   A    SP8

And I would like to only keep COL1 COL2 groups where there is at least two different SP names.

I would then get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

COL1 COL2 SP
G1   A    SP1
G1   A    SP2
G3   C    SP7
G3   C    SP3

>Solution :

Let us try transform with nunique

out = df[df.groupby(['COL1','COL2'])['SP'].transform('nunique')>1]
Out[245]: 
  COL1 COL2   SP
0   G1    A  SP1
1   G1    A  SP2
4   G3    C  SP7
5   G3    C  SP3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading