0

I would like to map the values in df1 dataframe to the df2 dataframe.

df:

id name
1001 john
1002 amy
1003 jim

df2:

name salary department
john 2000 A
amy 3000 B
jim 4000 C
john 5000 D
jim 6000 E
amy 7000 F

Output should be like this ,df3:

name id salary department
john 1001 2000 A
amy 1002 3000 B
jim 1003 4000 C
john 1001 5000 D
jim 1003 6000 E
amy 1002 7000 F
  • Take a look to [Pandas Merging 101](https://stackoverflow.com/q/53645882/15239951). The answer is `df2.merge(df1, on='name', how='left')` – Corralien Jan 10 '23 at 22:03
  • It use map when join on one column and returning one column. Something like `df2['id'] = df2['name'].map(df1.set_index('name')['id'])` Much faster and more performant that merge. – Scott Boston Jan 10 '23 at 22:15

0 Answers0