panda
panda

Reputation: 625

How to check not in on multiple dataframes pandas?

I have the following dataframes.

import pandas as pd
d={'P':['A','B','C'],
   'Q':[5,6,7]
  }
df=pd.DataFrame(data=d)
print(df)

d={'P':['A','C','D'],
   'Q':[5,7,8]
  }
df1=pd.DataFrame(data=d)
print(df1)

d={'P':['B','E','F'],
   'Q':[5,7,8]
  }
df3=pd.DataFrame(data=d)
print(df3)

Code to check one dataframe column not present in other is this:

df.loc[~df['P'].isin(df1['P'])]

How to check the same in multiple columns?

How to find P column in df3 not in P column of df and df1?

Expected Output:

    P   Q
0   E   7
1   F   8

Upvotes: 4

Views: 632

Answers (2)

Karn Kumar
Karn Kumar

Reputation: 8826

What about, However jezrael already given expert answer :)

You can simply define the conditions, and then combine them logically, like:

con1 = df3['P'].isin(df['P'])
con2 = df3['P'].isin(df1['P'])
df = df3[~ (con1 | con2)]
>>> df
   P  Q
1  E  7
2  F  8

Upvotes: 1

jezrael
jezrael

Reputation: 863166

You can chain 2 conditions with & for bitwise AND:

cond1 = ~df3['P'].isin(df1['P'])
cond2 = ~df3['P'].isin(df['P'])
df = df3.loc[cond1 & cond2]
print (df)
   P  Q
1  E  7
2  F  8

Or join values of columns - by concatenate or join list by +:

df = df3.loc[~df3['P'].isin(np.concatenate([df1['P'],df['P']]))]
#another solution 
#df = df3.loc[~df3['P'].isin(df1['P'].tolist() + df['P'].tolist())]

Upvotes: 4

Related Questions