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

Only keep row that do not contain values in two columns pandas

I have a dataframe and I want to only keep row where COL1 does not contains ("Danio_rerio") and COL2 does not contains ("Homo_sapiens")

So I used the following syntax:

df.loc[~ (df['COL1']=="Danio_rerio") & (df['COL2']=="Homo_sapiens")]

But it does not do what I whant, I’m missing something ?

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 :

Here need ~ for both conditions, so added () – it means remove rows if match "Danio_rerio" and Homo_sapiens:

df.loc[~ ((df['COL1']=="Danio_rerio") & (df['COL2']=="Homo_sapiens"))]

Or use De morgans laws and invert == to != and & to |:

df.loc[(df['COL1']!="Danio_rerio") | (df['COL2']!="Homo_sapiens")]

Doesn’t OP ask for both values shouldn’t be in the respective columns?

It means remove rows if match "Danio_rerio" or Homo_sapiens:

df.loc[~ ((df['COL1']=="Danio_rerio") | (df['COL2']=="Homo_sapiens"))]

df.loc[(df['COL1']!="Danio_rerio") & (df['COL2']!="Homo_sapiens")]
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