【发布时间】:2022-01-18 16:25:45
【问题描述】:
我从 API 接收到以下 json 文件:
{'get': '夹具/统计数据', “参数”:{“夹具”:“65”}, “错误”:[], “结果”:2, “分页”:{“当前”:1,“总”:1}, '响应':[{'团队':{'id':33, “名称”:“曼彻斯特联队”} 'statistics': [{'type': '射门次数', 'value': 6}, {'type': '射门次数', 'value': 1}, {'type': '总投篮次数', 'value': 8}, {'type': 'Blocked Shots', 'value': 1}]}, {'团队':{'id':46, “名称”:“莱斯特”} 'statistics': [{'type': '射门次数', 'value': 4}, {'type': '射门次数', 'value': 3}, {'type': 'Total Shots', 'value': 13}, {'type': 'Blocked Shots', 'value': 6}]}]}
我正在尝试将统计部分的数据与团队信息放在同一行中。
当我跑步时:
results_df = pd.json_normalize(results_json, record_path=["response"])
我明白了:
但是,当我跑步时
results_data = pd.json_normalize(results_json, record_path = ["response", "statistics"])
我明白了:
| | type | value |
|---|----------------|-------|
| 0 | Shots on Goal | 6 |
| 1 | Shots off Goal | 1 |
| 2 | Total Shots | 8 |
| 3 | Blocked Shots | 1 |
| 4 | Shots on Goal | 4 |
| 5 | Shots off Goal | 3 |
| 6 | Total Shots | 13 |
| 7 | Blocked Shots | 6 |
在上面,第 0 - 3 行对应于 team.id = 33,而第 4 - 7 行对应于 team.id - 46。
有什么方法可以将 json 的统计部分中的数据放入每个响应的正确行中?
【问题讨论】: