Python-检查df1列中的值是否存在于df2列中

Python-检查df1列中的值是否存在于df2列中,python,pandas,Python,Pandas,我有两个数据帧 df1 df2 现在,如果在df1中的键在df2中找到,那么新列将有一个值find,否则找不到 具有输出数据帧的df1变为 ID Key Result 1 A Not Found 2 B Found 3 C Found 4 D Found 我们如何利用熊猫来做到这一点?它不是按ID进行连接/合并/合并。用于: 使用合并的另一个解决方案 import pandas as pd import

我有两个数据帧

df1

df2

现在,如果在df1中的键在df2中找到,那么新列将有一个值find,否则找不到

具有输出数据帧的df1变为

  ID   Key   Result
1    A        Not Found
2    B        Found
3    C        Found
4    D        Found
我们如何利用熊猫来做到这一点?它不是按ID进行连接/合并/合并。

用于:


使用合并的另一个解决方案

import pandas as pd
import numpy as np

res = pd.merge(df1,df2,how="left",left_on="Key",right_on="Key",suffixes=('', '_'))

res["Result"] = np.where(pd.isna(res.ID_),"Not Found","Found")

del res["ID_"]

res

使用
合并
np.where

df1['Result'] = np.where(pd.merge(df1,df2,        
                           on='key',
                 suffixes=('_x', '_match'), 
                 how='left')['id_match'].fillna('Not Found')!='Not Found','Found','Not Found')

你可能想看看他的
.isin
df1['Result'] = np.where(df1['Key'].isin(df2['Key']), 'Found', 'Not Found')
print (df1)
   ID Key     Result
0   1   A  Not Found
1   2   B      Found
2   3   C      Found
3   4   D      Found
import pandas as pd
import numpy as np

res = pd.merge(df1,df2,how="left",left_on="Key",right_on="Key",suffixes=('', '_'))

res["Result"] = np.where(pd.isna(res.ID_),"Not Found","Found")

del res["ID_"]

res
df1['Result'] = np.where(pd.merge(df1,df2,        
                           on='key',
                 suffixes=('_x', '_match'), 
                 how='left')['id_match'].fillna('Not Found')!='Not Found','Found','Not Found')