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

I have a data frame A, like this:

enter image description here

And another data frame B, which looks like this:

enter image description here

I want to add the column "Exist" in data frame A, so that if the user and the movie exist in data frame B, then "Exist" is True, otherwise it is False. So A should look like this: enter image description here

+4
source share
1 answer

You can use mergewith a parameter indicator, then remove the column Ratingand 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
+5
source

Source: https://habr.com/ru/post/1650691/


All Articles