简体   繁体   中英

Making a dictionary from pandas dataframes

I have pandas data frame towns which is looking something like this:

**towns**
Paris
Berlin
London
etc..

Also, I have one more data frame totalPopulation which is looking something like this:

ID-cell    TOWNS      NumberOfPopulation
1          Paris       444
1          Berlin      333
1          London      111
2          Paris       222
2          London      555
3          Paris       999

And I need to create dictionary with a nested list, to get something like this:

'Paris' : [1, 444],[2,222],[3,999]
'Berlin': [1,333]
'London': [1,111], [2,555]

I tried to do something like this:

dictionary = {}
for town in towns.itertuples(index = False):
  dictionary[town] = totalPopulation.loc[totalPopulation['TOWNS'] == town].sort_values(totalPopulation.columns[2], ascending=False)

When I call the print method after the loop, I get a list of numbers, I supposed indices. And I expecting values. :D

EDIT: I just restart my computer (not for this reason :D) and run my program again. Now I get this:

{Pandas(town='Paris'): Empty DataFrame
Columns: [ID-cell, TOWNS, NumberOfPopulation]
Index: [], Pandas(Province='London'): Empty DataFrame
Columns: [ID-cell, TOWNS, NumberOfPopulation]
....}

And when I try

print(dictionary['Paris']) 

I get keyError

You can do with groupby and to_dict

df.groupby('TOWNS')['ID-cell','NumberOfPopulation'].apply(lambda x : x.values.tolist()).get(towns)
{'Berlin': [[1, 333]],
 'London': [[1, 111], [2, 555]],
 'Paris': [[1, 444], [2, 222], [3, 999]]}

This is one way, if you're OK with a list of tuples. Assumes your dataframe is already sorted by ID-cell and for each town this number is unique.

from functools import partial

res = df.groupby(['TOWNS'])['NumberOfPopulation']\
        .apply(partial(enumerate, start=1))\
        .apply(list)\
        .to_dict()

print(res)

{'Berlin': [(1, 333)],
 'London': [(1, 111), (2, 555)],
 'Paris': [(1, 444), (2, 222), (3, 999)]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM