Objective: convert pandas dataframe to an aggregated json-like object.
The "json-like" object contains an aggregate (sum) of the values for each Group and Category as weights.
Current state:
df = pd.DataFrame({'group': ["Group 1", "Group 1", "Group 2", "Group 3", "Group 3", "Group 3"],
'category': ["Category 1.1", "Category 1.2", "Category 2.1", "Category 3.1", "Category 3.2", "Category 3.3"],
'value': [2, 4, 5, 1, 4, 5]
})
Structure:
>>> df[['group','category','value']]
group category value
0 Group 1 Category 1.1 2
1 Group 1 Category 1.2 4
2 Group 2 Category 2.1 5
3 Group 3 Category 3.1 1
4 Group 3 Category 3.2 4
5 Group 3 Category 3.3 5
Desired output:
{"groups": [
{"label": "Group 1",
"weight": 6,
"groups": [
{"label": "Category 1.1",
"weight": 2,
"groups": [] },
{"label": "Category 1.2",
"weight": 4,
"groups": [] }
] },
{"label": "Group 2",
"weight": 5,
"groups": [{
"label": "Category 2.1",
"weight": 5,
"groups": []
} ] },
{"label": "Group 3",
"weight": 10,
"groups": [{
"label": "Category 3.1",
"weight": 1,
"groups": []
},
{"label": "Category 3.2",
"weight": 4,
"groups": []
},
{"label": "Category 3.3",
"weight": 5,
"groups": []
} ]
} ]
}
Tried so far:
pd.pivot_table(df, index=['group'],columns=['category'], values=['value'],aggfunc=np.sum, margins=True).stack('category')
Pivot output:
value
group category
Group 1 All 6.0
Category 1.1 2.0
Category 1.2 4.0
Group 2 All 5.0
Category 2.1 5.0
Group 3 All 10.0
Category 3.1 1.0
Category 3.2 4.0
Category 3.3 5.0
All All 21.0
Category 1.1 2.0
Category 1.2 4.0
Category 2.1 5.0
Category 3.1 1.0
Category 3.2 4.0
Category 3.3 5.0
From there I'm stuck. The rollup "All" seems like it should be in another column and I don't want it as a "group". I've tried using to_json()
with various iterations of record
, values
, split
as args, but I can't figure out how to render the desired output.
Also tried df.groupby(['group','category']).agg({'value':'sum'})
, but I don't get the rollup sum.
Questions similar, but not quite the structure I'm seeking: