I have two dataframe, df1
and df2
df1 = A B
1 a
1
1 5
1 b
1 c
1 d
df2 = A B C
1 a apple
1 cherry
1 5 apple
1 b orange
I want to merge these two dataframes based on A and B column. My logic is the following:
if df1['A'][0] is in df2['A'] and df1['B'][0] is in df2['B'] and they are equal:
then create new column df1['New Product'] = df2['C']
I need to create the third column in df1
if this condition is met.
I tried hard but without any success. I guess index position matters.
This is my solution which does not work:
df1['New Product'] = df2['C'][(df1['A'].isin(df2['A'])) & (df1['B'].isin(df2['B']))]
The expected output should be:
df1 = A B C
1 a apple
1 cherry
1 5 apple
1 b orange
1 c nan
1 d nan