如何将柱状形式的嵌套 JSON 转换为 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28319714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:54:44 来源:igfitidea点击:
how to convert this nested JSON in columnar form into Pandas dataframe
提问by user3675188
I could read this nested JSON format in columnar format into pandas.
我可以将这种嵌套的 JSON 格式以柱状格式读取到 Pandas 中。
JSON scheme format
JSON 方案格式


Python script
Python脚本
req = requests.get(REQUEST_API)
returned_data = json.loads(req.text)
# status
print("status: {0}".format(returned_data["status"]))
# api version
print("version: {0}".format(returned_data["version"]))
data_in_columnar_form = pd.DataFrame(returned_data["data"])
data = data_in_columnar_form["data"]
UPDATE
更新
I want to convert the following JSON scheme into the tabular format as the table, how to ?
我想将以下JSON方案转换为表格格式作为表格,如何?


JSON Scheme
JSON 方案
"data":[
{
"year":"2009",
"values":[
{
"Actual":"(0.2)"
},
{
"Upper End of Range":"-"
},
{
"Upper End of Central Tendency":"-"
},
{
"Lower End of Central Tendency":"-"
},
{
"Lower End of Range":"-"
}
]
},
{
"year":"2010",
"values":[
{
"Actual":"2.8"
},
{
"Upper End of Range":"-"
},
{
"Upper End of Central Tendency":"-"
},
{
"Lower End of Central Tendency":"-"
},
{
"Lower End of Range":"-"
}
]
},...
]
回答by Andy Hayden
Pandas has a JSON normalizationfunction (as of 0.13), straight out of the docs:
Pandas 有一个JSON 规范化函数(从 0.13 开始),直接从文档中提取:
In [205]: from pandas.io.json import json_normalize
In [206]: data = [{'state': 'Florida',
.....: 'shortname': 'FL',
.....: 'info': {
.....: 'governor': 'Rick Scott'
.....: },
.....: 'counties': [{'name': 'Dade', 'population': 12345},
.....: {'name': 'Broward', 'population': 40000},
.....: {'name': 'Palm Beach', 'population': 60000}]},
.....: {'state': 'Ohio',
.....: 'shortname': 'OH',
.....: 'info': {
.....: 'governor': 'John Kasich'
.....: },
.....: 'counties': [{'name': 'Summit', 'population': 1234},
.....: {'name': 'Cuyahoga', 'population': 1337}]}]
.....:
In [207]: json_normalize(data, 'counties', ['state', 'shortname', ['info', 'governor']])
Out[207]:
name population info.governor state shortname
0 Dade 12345 Rick Scott Florida FL
1 Broward 40000 Rick Scott Florida FL
2 Palm Beach 60000 Rick Scott Florida FL
3 Summit 1234 John Kasich Ohio OH
4 Cuyahoga 1337 John Kasich Ohio OH

