Him*_*nAB 17 python dataframe pandas
我有一个像这样的数据框A:
另一个数据框B看起来像这样:
我想在数据框A中添加一个"Exist"列,这样如果User和Movie都存在于数据框B中,那么'Exist'为True,否则为False.所以A应该是这样的:

jez*_*ael 17
您可以使用mergewith参数indicator,然后删除列Rating并使用numpy.where:
df = pd.merge(df1, df2, on=['User','Movie'], how='left', indicator='Exist')
df.drop('Rating', inplace=True, axis=1)
df['Exist'] = np.where(df.Exist == 'both', True, False)
print (df)
User Movie Exist
0 1 333 False
1 1 1193 True
2 1 3 False
3 2 433 False
4 3 54 True
5 3 343 False
6 3 76 True
Run Code Online (Sandbox Code Playgroud)