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

Time:08-12

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 ?

CodePudding user response:

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")]
  • Related