python - 规范化 Python 列表以将 JSON 数据放入表中
问题描述
我正在尝试使用以下 python 代码从 API 中提取信息并最终创建一个玩家表。我已经将数据大部分标准化到该级别,但我正在努力使用 [名册] 列表。
import requests
import json
import pandas as pd
from pandas.io.json import json_normalize
r1 = requests.get('https://statsapi.web.nhl.com/api/v1/teams/16?hydrate=franchise(roster(season=20182019,person(name,stats(splits=[yearByYear]))))')
data = r1.json()
df1 = json_normalize(data, 'teams',['teams.franchise'],errors='ignore')['franchise']
df2 = json_normalize(df1)['roster.roster']
df3 = pd.DataFrame(data=df2.index, columns=['Id'])
df4 = pd.DataFrame(data=df2.values, columns=['Players'])
df4
返回:
0 [{'person': {'id': 8470645, 'fullName': 'Corey...
关于我可以做些什么来从这个 API 中提取每个人到一个表中的任何想法?IE:
ID | fullName |
.. .....
.. .....
谢谢。
解决方案
Looks like the main problem is this is a deep dictionary. Some inspection showed that this code will get you to each player:
all_players = []
for team in data['teams']:
for player in team['franchise']['roster']['roster']:
player = player['person']
print(player.keys())
print(player)
print()
However, some of the keys in player
correspond to more dictionaries. So you'll either have to decide which player fields are basic values like strings/ints/etc and keep those, or add more code to parse out the additional dictionaries.
But this code will get you to each player, then you can normalize how you want from there.
Let me know if you need help!
推荐阅读
- android - 如何查看在连接到 Android Studio 之前记录的 Android 日志
- postgresql - 将数据转置为表格表示
- office-js - 检测 Word API 中文本的部分字体样式属性
- ios - 如何以编程方式按下 SearchBar 的取消按钮或将其返回到初始状态?
- android - Android 应用程序崩溃并在 rooturl 上显示错误
- javascript - 如何切换 a 的可见性
- c# - Azure 函数读取 local.settings.json 到对象
- ruby-on-rails - 每当 gem 错误 - 找不到命令“bin/rails”
- javascript - 模拟按键事件按下,强制按住向下箭头?
- python - Raspberry Pi 线程 RFID 上的 PyQt5 Python