I have a DF with structure as follows:
traffic_group app_id key category factors
0 desktop app1 CI html 16.618628
1 desktop app1 CI xhr 35.497082
2 desktop app1 IP html 18.294468
3 desktop app1 IP xhr 30.422464
4 desktop app2 CI html 11.028240
5 desktop app2 CI json 33.548279
6 mobile app1 IP html 12.808367
7 mobile app1 IP image 14.410633
I need to output it to a json of the following structure:
{ "desktop": {
app1: [ {
"key": "CI",
"threshold: 1,
"window": 60,
"factors: {
"html" : 16.618628
"xhr" : 35.497082
}
}, {
"key": "IP",
"threshold: 1,
"window": 60,
"factors: {
"html" : 18.294468
"xhr" : 30.422464
}
],
app2: [ {
"key": "CI",
"threshold: 1,
"window": 60,
"factors: {
"html" : 11.028240
"json" : 33.548279
}
}
},
"mobile": {
app1: [ {
"key": "IP",
"threshold: 1,
"window": 60,
"factors: {
"html" : 12.808367
"xhr" : 14.410633
}
]
}
}
The structure is admittedly convoluted.
I've considered the following previous answers and tried to mimic their logic to no avail:
Convert Pandas Dataframe to Custom Nested JSON
convert dataframe to nested json
Pandas Dataframe to Nested JSON
Any help is appreciated. Please don't just post a solution, but also explain your logic.