harlekin
harlekin

Reputation: 315

Python create new column from dictionary keys

I am trying to learn using Python for Data Science and got stuck with the following problem.

Suppose that we have a data frame of the form

enter image description here

and dictionary of the form

    Regions = {“Central Asia”: [“Afghanistan”], 
“Europe”: [“Albania”, “Andorra”], 
“Africa”: [“Algeria”], 
“America”: [“American Samoa”]}

I would like to create a new column in the data frame with keys from this dictionary being the entries, like this:

enter image description here

Is there a good way to do this in Python?

Thanks for your help!!

Upvotes: 3

Views: 2485

Answers (2)

cs95
cs95

Reputation: 403128

A one liner using DataFrame.from_dict and df.stack() (thanks MaxU!):

In [8]: pd.DataFrame.from_dict(Regions, orient='index').stack()\
                     .reset_index(level=0)\
                     .rename(columns={'level_0':'Region',0:'country'})\
                     .reset_index(drop=True)
Out[8]: 
         Region         country
0        Europe         Albania
1        Europe         Andorra
2        Africa         Algeria
3  Central Asia     Afghanistan
4       America  American Samoa

Upvotes: 5

jezrael
jezrael

Reputation: 863541

You can use map with dict where are swapped keys with values:

d = {k: oldk for oldk, oldv in Regions.items() for k in oldv}
print (d)
{'Andorra': 'Europe', 'Afghanistan': 'Central Asia',
 'Algeria': 'Africa', 'American Samoa': 'America', 'Albania': 'Europe'}

df['Region'] = df['country'].map(d)
print (df)
          country        Region
0     Afghanistan  Central Asia
1         Albania        Europe
2         Algeria        Africa
3  American Samoa       America
4         Andorra        Europe

Upvotes: 7

Related Questions