python - 如何在转换为 Dataframe 之前展平复杂的 json
问题描述
我正在尝试将复杂的 json 转换为 pandas 数据框,但是在转换它时遇到了问题。我可能不确定如何将复杂的 json 转换为 pandas 数据框。
我想显示数据框中的所有数据,这就是我获取它的方式,并且我正在共享我的 json 数据的格式,以便可以理解
{
'1164223': {
'fullSummaryLink': '/series/19014/scorecard/1164223/new-zealand-a-vs-india-a-1st-unofficial-odi-india-a-tour-of-nz-2018-19',
'innings': {
'1': {
'batsmen': [
{
'name': 'HD Rutherford',
'href': 'http://www.espncricinfo.com/ci/content/player/331375.html',
'stats': {
'runs': {
'name': 'runs',
'text': 'RUNS',
'value': '70'
},
'ballsFaced': {
'name': 'ballsFaced',
'text': 'BF',
'value': '66'
},
'notouts': {
'name': 'notouts',
'text': 'Not Out',
'value': '0'
}
}
},
{
'name': 'JDS Neesham',
'href': 'http://www.espncricinfo.com/ci/content/player/355269.html',
'stats': {
'runs': {
'name': 'runs',
'text': 'RUNS',
'value': '79'
},
'ballsFaced': {
'name': 'ballsFaced',
'text': 'BF',
'value': '48'
},
'notouts': {
'name': 'notouts',
'text': 'Not Out',
'value': '1'
}
}
}
],
'team': {
'teamDisplayName': 'NEW ZEALAND A',
'innDisplayName': 'INNINGS',
'runs': 308,
'overs': 50,
'wickets': 6,
'description': 'complete',
'inningsRunWicket': '308/6',
'inningStatus': ''
},
'bowlers': [
{
'name': 'S Kaul',
'href': 'http://www.espncricinfo.com/ci/content/player/326017.html',
'stats': {
'overs': {
'name': 'overs',
'text': 'O',
'value': '10'
},
'conceded': {
'name': 'conceded',
'text': 'R',
'value': '74'
},
'wickets': {
'name': 'wickets',
'text': 'E',
'value': '2'
}
}
},
{
'name': 'K Gowtham',
'href': 'http://www.espncricinfo.com/ci/content/player/424377.html',
'stats': {
'overs': {
'name': 'overs',
'text': 'O',
'value': '9'
},
'conceded': {
'name': 'conceded',
'text': 'R',
'value': '46'
},
'wickets': {
'name': 'wickets',
'text': 'E',
'value': '1'
}
}
}
]
},
'2': {
'bowlers': [
{
'name': 'HK Bennett',
'href': 'http://www.espncricinfo.com/ci/content/player/226493.html',
'stats': {
'overs': {
'name': 'overs',
'text': 'O',
'value': '10'
},
'conceded': {
'name': 'conceded',
'text': 'R',
'value': '65'
},
'wickets': {
'name': 'wickets',
'text': 'E',
'value': '2'
}
}
},
{
'name': 'LH Ferguson',
'href': 'http://www.espncricinfo.com/ci/content/player/493773.html',
'stats': {
'overs': {
'name': 'overs',
'text': 'O',
'value': '10'
},
'conceded': {
'name': 'conceded',
'text': 'R',
'value': '75'
},
'wickets': {
'name': 'wickets',
'text': 'E',
'value': '2'
}
}
}
],
'batsmen': [
{
'name': 'V Shankar',
'href': 'http://www.espncricinfo.com/ci/content/player/477021.html',
'stats': {
'runs': {
'name': 'runs',
'text': 'RUNS',
'value': '87'
},
'ballsFaced': {
'name': 'ballsFaced',
'text': 'BF',
'value': '80'
},
'notouts': {
'name': 'notouts',
'text': 'Not Out',
'value': '1'
}
}
},
{
'name': 'SS Iyer',
'href': 'http://www.espncricinfo.com/ci/content/player/642519.html',
'stats': {
'runs': {
'name': 'runs',
'text': 'RUNS',
'value': '54'
},
'ballsFaced': {
'name': 'ballsFaced',
'text': 'BF',
'value': '54'
},
'notouts': {
'name': 'notouts',
'text': 'Not Out',
'value': '0'
}
}
}
],
'team': {
'teamDisplayName': 'INDIA A',
'innDisplayName': 'INNINGS',
'runs': 311,
'overs': 49,
'wickets': 6,
'description': 'target reached',
'inningsRunWicket': '311/6',
'inningStatus': ''
}
}
},
'isAvailable': True
}
}
这是我的 python 代码,它将 json 值的字典转换为 pandas 数据框
df = pd.concat({k: pd.DataFrame(v) for k, v in scorecard_summary.items()})
df
解决方案
推荐阅读
- apache-flink - 作业失败时的 Flink 指标不是我在 Prometheus 中所期望的
- python - 将 Scala 代码转换为重命名和移动 CSV 文件 - Spark - PySpark
- c# - .net 核心中的 SendGrid 入站解析 Webhook
- excel - 我如何才能自动选择与每个月对应的参数?
- python - 在不知道其名称的情况下获得第一个通过的 kwarg 的 Pythonic 方式?
- c# - GraphQL.NET 动态字段类型
- javascript - 如果 props 是有效的 JSON,请检查 PropTypes
- elasticsearch - 在术语聚合中附加进一步的聚合
- javascript - 渲染没有父路由器视图的嵌套路由器视图 - React.js
- docker - Docker Windows 超时