1

I have this dictionary which I scraped from Wikipedia. The dictionary contains continents and their respective countries. For example:

theWorld = {'Asia': ['China', 'Malaysia'.. etc], 'Europe': ['Germany', 'Italy' ..etc]}

I am trying to make a dataframe using pandas to map a country to its continent. For example:

Country   Continent
China     Asia
Malaysia  Asia 
Germany   Europe
Ghana     Africa

and so on and so fort.

2 Answers 2

1

You could use a list comprehension to create the rows of the DataFrame:

import pandas as pd

theWorld = {'Asia': ['China', 'Malaysia'], 'Europe': ['Germany', 'Italy']}

df = pd.DataFrame(data=[[v, k] for k, vs in theWorld.items() for v in vs], columns=['country', 'continent'])
print(df)

Output

    country continent
0     China      Asia
1  Malaysia      Asia
2   Germany    Europe
3     Italy    Europe

Additional resources:

  • DataFrame data structure, here
  • When to use a list comprehension, here
Sign up to request clarification or add additional context in comments.

4 Comments

thank you, that solved my problem. Would you be so kind to tell me what kind of reference I should look into for the solution you provided.
I don't understand list comprehension yet but I converted to normal loop and I kind of understood what you did exactly so thank you again. data= [] for k, vs in theWorld.items(): for v in vs: data.append([v,k]) print(data)
@Ozeus I linked some resources to list comprehensions, hope it helps
really helpful. Thank you.
0

You could do it in two steps, first invert the dictionary in a dict comprehension then create the dataframe using that country to continent mapping:

>>> import pandas as pd
>>> the_world = {
...         'Asia': ['China', 'Malaysia'],
...         'Europe': ['Germany'],
...         'Africa': ['Ghana']
...     }
>>> country_to_continent = {
...         country: continent
...         for continent, countries in the_world.items() 
...         for country in countries
...     }
>>> country_to_continent
{'China': 'Asia', 'Malaysia': 'Asia', 'Germany': 'Europe', 'Ghana': 'Africa'}
>>> df = pd.DataFrame(
...         data={
...             'Country': country_to_continent.keys(),
...             'Continent': country_to_continent.values()
...         })
>>> df
    Country Continent
0     China      Asia
1  Malaysia      Asia
2   Germany    Europe
3     Ghana    Africa

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.