Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 536
harlekin Avatar asked Aug 19 '26 01:08

harlekin


2 Answers

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
like image 73
jezrael Avatar answered Aug 20 '26 14:08

jezrael


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
like image 42
cs95 Avatar answered Aug 20 '26 13:08

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!