【问题标题】:Check if a row in one data frame exist in another data frame检查一个数据框中的一行是否存在于另一个数据框中
【发布时间】:2016-08-09 23:54:13
【问题描述】:

我有一个这样的数据框A:

另一个数据框 B 如下所示:

我想在数据框 A 中添加一列“存在”,这样如果用户和电影都存在于数据框 B 中,则“存在”为真,否则为假。 所以 A 应该变成这样:

【问题讨论】:

  • 请不要对数据或表格使用 png,使用文本。

标签: python pandas dataframe


【解决方案1】:

您可以将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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2021-12-25
  • 2021-05-07
  • 1970-01-01
  • 2021-09-06
  • 2021-09-06
相关资源
最近更新 更多