简体   繁体   English

如何检查多个数据帧pandas?

[英]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? 如何在df3中找到P列而不是在df和df1的P列中?

Expected Output: 预期产出:

    P   Q
0   E   7
1   F   8

You can chain 2 conditions with & for bitwise AND : 您可以使用&为按位AND链接2个条件:

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 + : 或者连接列的值 - 通过concatenate或连接列表+

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 :) 怎么样,但是jezrael已经给了专家的答案:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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