我认为这种格式是简单的json,但我只需要“rows”字段作为数据帧:
{ "data":{
"filters":null,
"records":{
"totalrecords":3031,
"limit":3031,
"offset":0,
"data":{
"headers":{
"symbol":"SYMBOL",
"companyName":"NAME",
"lastSalePrice":"LAST PRICE",
"percentageChange":"% CHANGE",
"oneYearPercentagechange":"1 yr % CHANGE"
},
"rows":[
{
"oneYearPercentage":"11.20%",
"symbol":"KSCD",
"companyName":"KFA Small Cap Quality Dividend Index ETF",
"lastSalePrice":"$27.9998",
"netChange":"+0.4788",
"percentageChange":"1.74%",
"deltaIndicator":"up"
},
{
"oneYearPercentage":"58.70%",
"symbol":"KURE",
"companyName":"KraneShares MSCI All China Health Care Index ETF",
"lastSalePrice":"$41.62",
"netChange":"+1.47",
"percentageChange":"3.66%",
"deltaIndicator":"up"
}
]
}
} } }
如何仅将“rows”字段解析为Pandas数据帧,我尝试了以下方法:
import requests
import pandas as pd
import json
with open('etf-data.json') as json_file:
data = json.load(json_file)
print(data['data']['rows'])
df = pd.DataFrame.from_dict(pd.json_normalize(data), orient='columns')
但我不确定要正确解析这里的字段需要多少层
对于这样的嵌套json,我建议使用jmespath;它可以用更干净的外观来抽象一些索引:
编辑:我在这里再次看到JSON后就明白了。我的解决办法如下:
其输出为:
尝试递归调用键搜索函数
相关问题 更多 >
编程相关推荐