0

I have two dataset as below:

df1:

   col1     col2    new
0  cars       2     a
1  audi       1     b
2  benz       5     d
3  bmw        3     c

df2:

    col2    col3
0   audi     1
1   cars     4
2   bmw      3

Now I want to add new column in df2 such as where value of col2 & col3 of df2 matches exactly with col1 & col2 of df1 then add value of correspondin column new value to resultant new column or else add None.

Desired Output:

df2:

    col2    col3  value
0   audi     1      a
1   cars     4    None
2   bmw      3      c
Henry Ecker
  • 31,792
  • 14
  • 29
  • 50
Chris_007
  • 631
  • 7
  • 19
  • Right merge -> `df2 = df1.merge(df2.rename(columns={'col2': 'col1', 'col3': 'col2', 'new': 'value'}), how='right', on=['col1', 'col2'])` – Henry Ecker Jul 26 '21 at 03:03

0 Answers0