我有一个这样的数据框A:
另一个数据框 B 看起来像这样:
我想向数据框 A 添加一列“存在”,这样如果用户和电影都存在于数据框 B 中,则“存在”为 True,否则为 False。 所以A应该变成这样:
最佳答案
您可以使用 merge
使用参数 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
关于python - 检查一个数据框中的一行是否存在于另一个数据框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38855204/