简体   繁体   中英

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

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())]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM