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

Question:

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 ?

Asked By: chippycentra

||

Answers:

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")]
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.