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

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:

Is there a good way to do this in Python?
Thanks for your help!!
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With