deuwde deuwde - 8 months ago 50
Python Question

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 to map?

Top15 is an existing dataframe with Country as the Index.

ContinentDict = {'China':'Asia',
'United States':'North America',
'United Kingdom':'Europe',
'Russian Federation':'Europe',
'Canada':'North America',
'South Korea':'Asia',
'Brazil':'South America'}

Top15['Continent'] = Top15['Country'].map(ContinentDict)

Answer Source

This ought to work. Here's an example:

In [1]:Top15 = pd.DataFrame({'Country':['France','Brazil', 'Canada', 'Japan']})
0  France
1  Brazil
2  Canada
3   Japan

Now you can indeed use using a dict as argument:

In [2]: Top15['Continent'] = Top15['Country'].map(ContinentDict)
  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 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'] = x: ContinentDict[x])