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 if User and Movie both exist in data frame B then 'Exist' is True, otherwise it is False. So A should become like this:
You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas. series. isin() , str.
Difference between rows or columns of a pandas DataFrame object is found using the diff() method. The axis parameter decides whether difference to be calculated is between rows or between columns. When the periods parameter assumes positive values, difference is found by subtracting the previous row from the next row.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With