Skip to content
Advertisement

Converting dataframe to dictionary with country by continent

I have a .csv and dataframe which has 2 columns (country, continent). I want to create a dictionary, carrying the continent as key and a list of all countries as values.

The .csv has the following format:

country continent
Algeria Africa
Angola Africa

and so on.

I tried using:

continentsDict = dict([(con, cou) for con, cou in zip(continents.continent, continents.country)])

But this gave me the following output:

{'Africa': 'Zimbabwe', 'Asia': 'Yemen', 'Europe': 'Vatican City', 'North America': 'United States Virgin Islands', 'Oceania': 'Wallis and Futuna', 'South America': 'Venezuela'}

Which is the right format but only added the last value it found for the respective continent.

Anyone an idea?

Thank you!

Advertisement

Answer

Assuming continents is the instance of your pandas df, you could do:

continentsDict = continents.groupby("continent")["country"].apply(list).to_dict()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement