首页 > 解决方案 > 规范化 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 |
..   .....
..   .....

谢谢。

标签: pythonjsonpandasapi

解决方案


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!


推荐阅读