César Barboza
César Barboza

Reputation: 187

How to build a dictionary with the values of each country in the same group

I have the following data in a dictionary or a list:

{'time': '2016 Nov', 'value': 8.0, 'Country': 'Denmark'}
{'time': '2016 Dec', 'value': 8.5, 'Country': 'Denmark'}
{'time': '2017 Jan', 'value': 11.9, 'Country': 'Denmark'}
{'time': '2017 Feb', 'value': 13.5, 'Country': 'Denmark'}
{'time': '2017 Mar', 'value': 13.9, 'Country': 'Denmark'}
{'time': '2017 Apr', 'value': 14.6, 'Country': 'Denmark'}
{'time': '2017 May', 'value': 13.4, 'Country': 'Denmark'}
{'time': '2017 Jun', 'value': 15.0, 'Country': 'Denmark'}
{'time': '2017 Jul', 'value': 16.6, 'Country': 'Denmark'}
{'time': '2017 Aug', 'value': 14.7, 'Country': 'Denmark'}
{'time': '2017 Sep', 'value': 16.2, 'Country': 'Denmark'}
{'time': '2017 Oct', 'value': 14.9, 'Country': 'Denmark'}
{'time': '2016 Nov', 'value': 19.9, 'Country': 'Finland'}
{'time': '2016 Dec', 'value': 22.4, 'Country': 'Finland'}
{'time': '2017 Jan', 'value': 22.6, 'Country': 'Finland'}
{'time': '2017 Feb', 'value': 20.9, 'Country': 'Finland'}
{'time': '2017 Mar', 'value': 23.2, 'Country': 'Finland'}
{'time': '2017 Apr', 'value': 23.6, 'Country': 'Finland'}
{'time': '2017 May', 'value': 24.5, 'Country': 'Finland'}
{'time': '2017 Jun', 'value': 24.3, 'Country': 'Finland'}
{'time': '2017 Jul', 'value': 24.7, 'Country': 'Finland'}
{'time': '2017 Aug', 'value': 25.5, 'Country': 'Finland'}
{'time': '2017 Sep', 'value': 27.1, 'Country': 'Finland'}
{'time': '2017 Oct', 'value': 27.2, 'Country': 'Finland'}

I would like to know how can I buld a dictionary that for each date, it shows the value for each country like this:

{'time': '2016 Nov', 'Denmark': 8.0, 'Finland': 19.9 },
{'time': '2016 Dec', 'Denmark': 8.5, 'Finland': 22.4 },

And so on...

Upvotes: 2

Views: 62

Answers (2)

jezrael
jezrael

Reputation: 862851

If input is list of dictionaries first create DataFrame and then pivot, last convert to dict by to_dict:

d = pd.DataFrame(d).pivot('time','Country','value').reset_index().to_dict('r')

Or use set_index with unstack:

d=pd.DataFrame(d).set_index(['time','Country'])['value'].unstack().reset_index().to_dict('r')

print (d)
[{'Denmark': 8.5, 'time': '2016 Dec', 'Finland': 22.4}, 
 {'Denmark': 8.0, 'time': '2016 Nov', 'Finland': 19.9}, 
 {'Denmark': 14.6, 'time': '2017 Apr', 'Finland': 23.6}...

Detail:

#check first 5 rows of DataFrame
print (pd.DataFrame(d).head())
   Country      time  value
0  Denmark  2016 Nov    8.0
1  Denmark  2016 Dec    8.5
2  Denmark  2017 Jan   11.9
3  Denmark  2017 Feb   13.5
4  Denmark  2017 Mar   13.9

print (pd.DataFrame(d).pivot('time','Country','value'))
Country   Denmark  Finland
time                      
2016 Dec      8.5     22.4
2016 Nov      8.0     19.9
2017 Apr     14.6     23.6
2017 Aug     14.7     25.5
2017 Feb     13.5     20.9
2017 Jan     11.9     22.6
2017 Jul     16.6     24.7
2017 Jun     15.0     24.3
2017 Mar     13.9     23.2
2017 May     13.4     24.5
2017 Oct     14.9     27.2
2017 Sep     16.2     27.1

EDIT:

For sorting convert dates to datetimes and then reindex:

df = pd.DataFrame(d).pivot('time','Country','value')
sort = pd.to_datetime(df.index).sort_values().strftime('%Y %b')
d = df.reindex(sort).reset_index().to_dict('r')
print (d)

Detail:

print (df.reindex(sort))
Country   Denmark  Finland
time                      
2016 Nov      8.0     19.9
2016 Dec      8.5     22.4
2017 Jan     11.9     22.6
2017 Feb     13.5     20.9
2017 Mar     13.9     23.2
2017 Apr     14.6     23.6
2017 May     13.4     24.5
2017 Jun     15.0     24.3
2017 Jul     16.6     24.7
2017 Aug     14.7     25.5
2017 Sep     16.2     27.1
2017 Oct     14.9     27.2

Upvotes: 2

piRSquared
piRSquared

Reputation: 294348

Using cytoolz.dicttoolz.merge and defaultdict

from cytoolz.dicttoolz import merge
from collections import defaultdict

d = defaultdict(lambda: defaultdict(dict))

for r in lod:
    d[r['time']][r['Country']] = r['value']

[merge({'time': k}, v) for k, v in d.items()]

[{'Denmark': 8.0, 'Finland': 19.9, 'time': '2016 Nov'},
 {'Denmark': 8.5, 'Finland': 22.4, 'time': '2016 Dec'},
 {'Denmark': 11.9, 'Finland': 22.6, 'time': '2017 Jan'},
 {'Denmark': 13.5, 'Finland': 20.9, 'time': '2017 Feb'},
 {'Denmark': 13.9, 'Finland': 23.2, 'time': '2017 Mar'},
 {'Denmark': 14.6, 'Finland': 23.6, 'time': '2017 Apr'},
 {'Denmark': 13.4, 'Finland': 24.5, 'time': '2017 May'},
 {'Denmark': 15.0, 'Finland': 24.3, 'time': '2017 Jun'},
 {'Denmark': 16.6, 'Finland': 24.7, 'time': '2017 Jul'},
 {'Denmark': 14.7, 'Finland': 25.5, 'time': '2017 Aug'},
 {'Denmark': 16.2, 'Finland': 27.1, 'time': '2017 Sep'},
 {'Denmark': 14.9, 'Finland': 27.2, 'time': '2017 Oct'}]

Setup

lod = [
    {'time': '2016 Nov', 'value': 8.0, 'Country': 'Denmark'},
    {'time': '2016 Dec', 'value': 8.5, 'Country': 'Denmark'},
    {'time': '2017 Jan', 'value': 11.9, 'Country': 'Denmark'},
    {'time': '2017 Feb', 'value': 13.5, 'Country': 'Denmark'},
    {'time': '2017 Mar', 'value': 13.9, 'Country': 'Denmark'},
    {'time': '2017 Apr', 'value': 14.6, 'Country': 'Denmark'},
    {'time': '2017 May', 'value': 13.4, 'Country': 'Denmark'},
    {'time': '2017 Jun', 'value': 15.0, 'Country': 'Denmark'},
    {'time': '2017 Jul', 'value': 16.6, 'Country': 'Denmark'},
    {'time': '2017 Aug', 'value': 14.7, 'Country': 'Denmark'},
    {'time': '2017 Sep', 'value': 16.2, 'Country': 'Denmark'},
    {'time': '2017 Oct', 'value': 14.9, 'Country': 'Denmark'},
    {'time': '2016 Nov', 'value': 19.9, 'Country': 'Finland'},
    {'time': '2016 Dec', 'value': 22.4, 'Country': 'Finland'},
    {'time': '2017 Jan', 'value': 22.6, 'Country': 'Finland'},
    {'time': '2017 Feb', 'value': 20.9, 'Country': 'Finland'},
    {'time': '2017 Mar', 'value': 23.2, 'Country': 'Finland'},
    {'time': '2017 Apr', 'value': 23.6, 'Country': 'Finland'},
    {'time': '2017 May', 'value': 24.5, 'Country': 'Finland'},
    {'time': '2017 Jun', 'value': 24.3, 'Country': 'Finland'},
    {'time': '2017 Jul', 'value': 24.7, 'Country': 'Finland'},
    {'time': '2017 Aug', 'value': 25.5, 'Country': 'Finland'},
    {'time': '2017 Sep', 'value': 27.1, 'Country': 'Finland'},
    {'time': '2017 Oct', 'value': 27.2, 'Country': 'Finland'}
]

Upvotes: 2

Related Questions