Home > Mobile >  Pandas data frame to dictionary using dict comprehension
Pandas data frame to dictionary using dict comprehension

Time:10-31

I have a data frame of cities and info about them. I need to make a dictionary with the countries where the cities are located as keys of my dictionary and a list of the cities in that country as values of my dictionary. All of this using dictionary comprehension and the columns df['country'] and df['city'].

I've tried so many things but always getting the error that pandas series are mutable and non hashable. Tried converting it into list of strings and getting the same error. Furthermore I dont know how to make an 'if' in the dict comprehension to include the city in the value list of its country key.

I have: `

key = df['country'].tolist()
value = df['city']
Megacities_dict = {key: value for value in df if key == df['country'] }

` And I expected to have an output like:

{'Japan': ['Osaka', 'Tokyo', ...], 'China': ['Beijing'...], ...}

But instead im getting the error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

CodePudding user response:

Simple. just groupby country and aggregate cities using list then use to_dict to get the dict

df.groupby('country')['city'].agg(list).to_dict()
  • Related