I have a dataframe and some of the values need to be updated. However, I want to leave the values without a key value as it currently is.
I tried other code but when I run the code it changes all the values without a key pair to NaN.
How do I go from
Df
City Employer
Toronto Magna's Automotive
Toronto Manga
Vancouver Dunder
Calgary Flames
mapping file
d = {'Manga':"Magna's Automotive",
'Dunder': 'Dunder Mifflin',}
Df-desired
City Employer
Toronto Magna's Automotive
Toronto Magna's Automotive
Vancouver Dunder Mifflin
Calgary Flames
CodePudding user response:
You can use:
df['Employer'] = df['Employer'].map(d).fillna(df['Employer'])
output:
City Employer
0 Toronto Magna's Automotive
1 Toronto Magna's Automotive
2 Vancouver Dunder Mifflin
3 Calgary Flames
Or, using update
for in place modification:
df.update(df['Employer'].map(d))