Following on from my previous question Working out summaries) …
Here’s the code I have so far :
import pandas
import numpy as np
data = [
{'group_name': 'GROUP 1', 'client': 'CLIENT 1', 'messages': 100, 'supplier': 'SUP 1', 'blocked': 0},
{'group_name': 'GROUP 2', 'client': 'CLIENT 1', 'messages': 200, 'supplier': 'SUP 1', 'blocked': 27},
{'group_name': 'GROUP 3', 'client': 'CLIENT 1', 'messages': 300, 'supplier': 'SUP 1', 'blocked': 0},
{'group_name': 'GROUP 1', 'client': 'CLIENT 2', 'messages': 400, 'supplier': 'SUP 1', 'blocked': 4},
{'group_name': 'GROUP 2', 'client': 'CLIENT 2', 'messages': 500, 'supplier': 'SUP 1', 'blocked': 0},
{'group_name': 'GROUP 4', 'client': 'CLIENT 3', 'messages': 600, 'supplier': 'SUP 1', 'blocked': 9},
]
newdata = {}
newdata['client'] = []
newdata['supplier'] = []
newdata['group_name'] = []
newdata['messages'] = []
newdata['blocked'] = []
for d in data:
newdata['client'].append(d['client'])
newdata['supplier'].append(d['supplier'])
newdata['group_name'].append(d['group_name'])
newdata['messages'].append(d['messages'])
newdata['blocked'].append(d['blocked'])
df = pandas.DataFrame(newdata)
grouped = df.groupby(['client','supplier'])
res = grouped[['messages','blocked']].agg(np.sum)
print(res.to_dict("records"))
and it produces this :
[
{
'messages': 600,
'blocked': 27
},
{
'messages': 900,
'blocked': 4
},
{
'messages': 600,
'blocked': 9
}
]
I want the respective “client” and “supplier” to be added to each record, like this :
[
{
'client': 'CLIENT 1',
'supplier': 'SUP 1',
'messages': 600,
'blocked': 27
},
{ etc etc
but I cannot work it out. Can anyone help?
edit sorry I did not explain the intended result - each record should have two non-aggregated fields (client and supplier) and two aggregated fields (messages & blocked). I don’t really get pandas (or itertools groupby), and I think I’m just being a bit slow this month.