检查一个数据帧中的行是否在另一个数据帧中 [英] Check if a row in one data frame exist in another data frame
本文介绍了检查一个数据帧中的行是否在另一个数据帧中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数据帧A:
I have a data frame A like this:
还有另一个数据帧B,如下所示:
And another data frame B which looks like this:
我想在数据框A中添加一列存在",以便如果用户和电影都存在于数据框B中,则存在"为True,否则为False. 因此,A应该变成这样:
I want to add a column 'Exist' to data frame A so that if User and Movie both exist in data frame B then 'Exist' is True, otherwise it is False. So A should become like this:
推荐答案
您可以使用indicator的="noreferrer"> merge
,然后删除列Rating
并使用
You can use merge
with parameter indicator
, then remove column Rating
and use 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
这篇关于检查一个数据帧中的行是否在另一个数据帧中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文