Given df1 and df2 dataframes, I would like to substitute the 'Name' items in df2 for their corresponding 'Area' values in df1.
In other words, the values in df2['Name] should be respectively A1 A3 A2 A1 A2
import pandas as pd
df1 = pd.DataFrame({'Name':['Tom', 'nick', 'Albert', 'Josh'],
'Area':['A1','A2','A3','A1']
})
df2 = pd.DataFrame({'Name':['Tom', 'Albert', 'Nick', 'Josh','Nick'],
'pet':['dog','cat','mouse','dog','mouse']
})
After searching for some suggestions, I added this line:
df = df2.merge(df1, on=['Area','Name'], how='left')
print(df)
But I get this error:
# Check for duplicates
KeyError: 'Area'
and this line:
df = df2['Name'].map(df1.set_index('Area')['Name'])
print(df)
outputs this error
InvalidIndexError: Reindexing only valid with uniquely valued Index objects