58

I have a data frame A dfA like this:

enter image description here

And another data frame B dfB which looks like this:

enter image description here

I want to add a column 'Exist' to dfA so that if User and Movie both exist in dfB then 'Exist' is True, otherwise it is False. So dfA should become like this:

enter image description here

1
  • 17
    Please dont use png for data or tables, use text. Commented Aug 9, 2016 at 16:16

1 Answer 1

72

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
Sign up to request clarification or add additional context in comments.

1 Comment

It will be useful to indicate that the objective of the OP requires a left outer join. More details here: realpython.com/pandas-merge-join-and-concat/#how-to-merge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.