Check if a row in one data frame exist in another data frame

前端 未结 1 1931
情话喂你
情话喂你 2020-12-03 01:18

I have a data frame A like this:

And another data frame B which looks like this:

I want to add a column \'Exist\' to data frame A so that i

相关标签:
1条回答
  • 2020-12-03 01:41

    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
    
    0 讨论(0)
提交回复
热议问题