【发布时间】:2019-12-23 20:31:05
【问题描述】:
我有一个结构如下的DF:
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
我需要将其输出到以下结构的json:
{ "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
}
]
}
}
这个结构无疑是错综复杂的。
我已经考虑了以下以前的答案,并试图模仿他们的逻辑无济于事:
Convert Pandas Dataframe to Custom Nested JSON
convert dataframe to nested json
Pandas Dataframe to Nested JSON
感谢任何帮助。请不要只是发布解决方案,还要解释您的逻辑。
【问题讨论】:
标签: python json pandas dataframe