【发布时间】:2021-03-30 14:59:42
【问题描述】:
我正在尝试使用 json_normalize 函数将 json 文件转换为数据帧。
源 JSON
-
json 是一个字典列表,看起来像这样:
{ "sport_key": "basketball_ncaab", "sport_nice": "NCAAB", "teams": [ "Bryant Bulldogs", "Wagner Seahawks" ], "commence_time": 1608152400, "home_team": "Bryant Bulldogs", "sites": [ { "site_key": "marathonbet", "site_nice": "Marathon Bet", "last_update": 1608156452, "odds": { "h2h": [ 1.28, 3.54 ] } }, { "site_key": "sport888", "site_nice": "888sport", "last_update": 1608156452, "odds": { "h2h": [ 1.13, 5.8 ] } }, { "site_key": "unibet", "site_nice": "Unibet", "last_update": 1608156434, "odds": { "h2h": [ 1.13, 5.8 ] } } ], "sites_count": 3 }
问题是未来的列之一包含一个列表(应该是这种情况),但是在 json_normalize 函数的元部分中包含此列会引发以下错误:
ValueError: operands could not be broadcast together with shape (22,) (11,)
当我尝试在以下代码的列表中添加“团队”时出现错误:
pd.json_normalize(data, 'sites', ['sport_key', 'sport_nice', 'home_team', 'teams'])
【问题讨论】:
标签: python json pandas normalize