3

I have two data frames as below:

df1:
col1    col2    col3
  1       2       A
  1       2       A
  3       4       B
  3       4       B

df2:
 col1    col2 
  1       2
  3       4

I want to put the value of col3 of df1 in df2 like below:

result:
col1    col2    col3
 1        2       A
 3        4       B

I tried with following code for mapping but getting error message, How to do that

df2['col3'] = df2[['col1','col2]].map(df1.set_index(['col1','col2])['col3'])

1 Answer 1

2

Use:

df11 = df1[['col1','col2','col3']].drop_duplicates(['col1','col2'])
df = df2.merge(df11, on=['col1','col2'], how='left')
print (df)
   col1  col2 col3
0     1     2    A
1     3     4    B
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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