Converting dataframe to dictionary with country by continent

Question:

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!

Asked By: Karim Abou El Naga

||

Answers:

Given:

   country continent
0  Algeria    Africa
1   Angola    Africa

Doing:

out = df.groupby('continent')['country'].agg(list).to_dict()
print(out)

Output:

{'Africa': ['Algeria', 'Angola']}
Answered By: BeRT2me

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

continentsDict = continents.groupby("continent")["country"].apply(list).to_dict()
Answered By: Bastián Bas
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.