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

Python (pandas) – How to group values in one column and then delete or keep that group based on values in another column

Let’s say I have the following pandas dataset:

Column 1 Column 2
dog RE
dog RE FX
cat RE BA
mouse AQ
mouse RE FX
salmon AQ

Essentially what I would like to do is group the values in Column 1 and then either keep or delete them based on the values in Column 2. So for example, I want to delete all values in a group in Column 1 if ANY of the corresponding rows in Column 2 are "RE" or "RE BA". Based on the dataset above, the output would be the following:

Column 1 Column 2
mouse AQ
mouse RE FX
salmon AQ

I am struggling with this because although I understand how to drop rows based on whether they contain a specific value, I don’t understand how to drop entire groups based on whether ANY of the rows in that group contain a specific value. Any help would be greatly appreciated!

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

>Solution :

You can try groupby then filter

out = df.groupby("Column 1").filter(lambda df: ~df['Column 2'].isin(["RE", "RE BA"]).any())
print(out)

  Column 1 Column 2
3    mouse       AQ
4    mouse    RE FX
5   salmon       AQ
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