我希望在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"
},
{
我最终希望将所有这些结果放入具有以下结构的数据帧中:
| 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)
任何见解都将不胜感激
试试
json_normalize()
+melt()
+explode()
+Dataframe()
:或
另一种方式是通过
stack()
+drop()
组合代替melt()
和rest,所有方法保持不变:df
的输出:相关问题 更多 >
编程相关推荐