I'm looking for a solution to convert a Pandas DataFrame with 3 subcategories to an JSON output without any lists.
This is the data-structure I got:
import pandas as pd
df = pd.DataFrame({
'cat1': ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'],
'cat2': ['BB', 'BB', 'BC', 'BB', 'BB', 'BB', 'BC', 'BC'],
'cat3': ['CC', 'CC', 'CD', 'CD', 'CD', 'CC', 'CD', 'CE'],
'prod': ['P1', 'P2', 'P3', 'P1', 'P4', 'P1', 'P3','P6'],
'amount': [132, 51, 12, 421, 55, 11, 123, 312]
})
And this is the desired JSON output:
{
"A": {
"BB": {
"CC": {
"P1": 132,
"P2": 51
}
},
"BC": {
"CD": {
"P3": 12
}
}
},
"B": {
"BB": {
"CD": {
"P1": 421,
"P4": 55
}
}
},
"C": {
"BB": {
"CC": {
"P1": 11
}
},
"BC": {
"CD": {
"P3": 123
},
"CE": {
"P6": 312
}
}
}
I had several attemts with the to_json
method and tried to adapt this answer without any luck.
def f(x):
return dict({k:v for k,v in zip(x.prod, x.amount)})
(
df.groupby(['cat1','cat2', 'cat3'])
.apply(f)
.groupby(level=0)
.apply(lambda x: x.tolist())
.to_dict()
)
TypeError: zip argument #1 must support iteration