Reputation: 1992
i have following dataframe format
name,state,country
a,1,67
b,2,52
i have following state code and country code mapping dictionary
state_map = { 1:'tn', 2:'kerala' }
country_map = { 67: 'usa', 52: 'india'
Upvotes: 0
Views: 1606
Reputation: 31
The above solution takes lot of time, hence use below code instead for better performance(Note fillna used to fillup default values when match not found):-
df['state'].map(state_map).fillna(0)
df['country'].map(country_map).fillna(0)
Upvotes: 0
Reputation: 101
From the documentation if giving a dict to replace:
So for your case your dict looks like:
r_map = {'state':{'1':'tn', '2':'kerala'},'country':{'67':'usa', '52':'india'}}
Use it like this:
df.replace(r_map)
Upvotes: 2