Mapping A Dictionary To A Dataframe Not Working Correctly
I am trying to map a dictionary to a dataframe. I dug through some code and pieced together what I thought would work, but the code isn't running. Can anyone help with getting this
Solution 1:
This ought to work. Here's an example:
In [1]:Top15 = pd.DataFrame({'Country':['France','Brazil', 'Canada', 'Japan']})
Top15
Out[1]:
Country
0 France
1 Brazil
2 Canada
3 Japan
Now you can indeed use pd.Series.map using a dict as argument:
In [2]: Top15['Continent'] = Top15['Country'].map(ContinentDict)
Top15
Out[2]:
Country Continent
0 France Europe
1 Brazil South America
2 Canada North America
3 Japan Asia
Update: now that we know Top15 is indexed by country
The problem is that index.map
doesn't allow a dict as an argument. But you can do either of these:
# 1000 loops, best of 3: 696 µs per loop
Top15['Continent'] = Top15.index.to_series().map(ContinentDict)
# 1000 loops, best of 3: 722 µs per loop
Top15['Continent'] = pd.Series(Top15.index).map(ContinentDict)
Or much faster:
# 10000 loops, best of 3: 156 µs per loop
Top15['Continent'] = Top15.index.map(lambda x: ContinentDict[x])
Post a Comment for "Mapping A Dictionary To A Dataframe Not Working Correctly"