【发布时间】:2021-10-12 12:41:06
【问题描述】:
我希望只提取 Python 中 JSON 的最低级别对象。例如,API 的前几条记录如下所示:
{
"season": 2021,
"charts": {
"ARI": {
"TE": [
{
"team": "ARI",
"position": "TE",
"depth": "1",
"playerId": "1443",
"name": "Dan Arnold"
},
{
"team": "ARI",
"position": "TE",
"depth": "2",
"playerId": "599",
"name": "Maxx Williams"
}
],
"K": [
{
"team": "ARI",
"position": "K",
"depth": "1",
"playerId": "1121",
"name": "Zane Gonzalez"
},
{
我最终希望将所有这些结果放入具有这种结构的 DataFrame 中:
| team | position | depth | playerId | name |
|:---- |:-------- |:----- |:-------- |:---- |
我尝试了以下代码的变体但没有成功:
import requests as rq
import pandas as pd
# Retrieve Depth Charts
json_depthCharts = rq.get(f"https://api.fantasynerds.com/v1/nfl/depth?apikey={API_KEY}").json()
df_depthCharts = pd.json_normalize(json_depthCharts, 'charts', ['charts', 'team'])
print(df_depthCharts)
感谢任何见解!
【问题讨论】:
-
此外,您提供的链接很有帮助,但您的 API 密钥在其中可见,因此请删除它,如果您有如此大的数据,请在未来将其粘贴到 pastebin 等网站或通过任何其他不利用敏感信息的媒体
-
先生,您的问题解决了吗?
标签: python json pandas dataframe extract