使用json_normalize展平嵌套json

2024-06-25 22:39:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python(Pandas)中的json-normalize压平json文件,但作为一个新手,我似乎总是以一个KeyError结尾。

我想实现的是一个游戏中所有游戏的数据帧。

我尝试过很多不同的路径和前缀,但都没有成功。谷歌搜索了很多,但我还是不够。

我想得到的是一个数据帧,如: 句点,时间,类型,player1,player2,xcord,ycord

import pandas as pd
import json

with open('PlayByPlay.json') as data_file:    
    data = json.load(data_file)

from pandas.io.json import json_normalize
records = json_normalize(data)

plays = records['data.game.plays.play'][0]
plays

会产生

{'aoi': [8470324, 8473449, 8475158, 8475215, 8477499, 8477933],
 'apb': [],
 'as': 0,
 'asog': 0,
 'desc': 'Zack Kassian hit Kyle Okposo',
 'eventid': 7,
 'formalEventId': 'EDM7',
 'hoi': [8471678, 8475178, 8475660, 8476454, 8476457, 8476472],
 'hpb': [],
 'hs': 0,
 'hsog': 0,
 'localtime': '5:12 PM',
 'p1name': 'Zack Kassian',
 'p2name': 'Kyle Okposo',
 'p3name': '',
 'period': 1,
 'pid': 8475178,
 'pid1': 8475178,
 'pid2': 8473449,
 'pid3': '',
 'playername': 'Zack Kassian',
 'strength': 701,
 'sweater': '44',
 'teamid': 22,
 'time': '00:28',
 'type': 'Hit',
 'xcoord': 22,
 'ycoord': 38}

Json格式

     {'data': {'game': {'awayteamid': 7,
   'awayteamname': 'Buffalo Sabres',
   'awayteamnick': 'Sabres',
   'hometeamid': 22,
   'hometeamname': 'Edmonton Oilers',
   'hometeamnick': 'Oilers',
   'plays': {'play': [{'aoi': [8470324,
       8473449,
       8475158,
       8475215,
       8477499,
       8477933],
      'apb': [],
      'as': 0,
      'asog': 0,
      'desc': 'Zack Kassian hit Kyle Okposo',
      'eventid': 7,
      'formalEventId': 'EDM7',
      'hoi': [8471678, 8475178, 8475660, 8476454, 8476457, 8476472],
      'hpb': [],
      'hs': 0,
      'hsog': 0,
      'localtime': '5:12 PM',
      'p1name': 'Zack Kassian',
      'p2name': 'Kyle Okposo',
      'p3name': '',
      'period': 1,
      'pid': 8475178,
      'pid1': 8475178,
      'pid2': 8473449,
      'pid3': '',
      'playername': 'Zack Kassian',
      'strength': 701,
      'sweater': '44',
      'teamid': 22,
      'time': '00:28',
      'type': 'Hit',
      'xcoord': 22,
      'ycoord': 38},
     {'aoi': [8471742, 8475179, 8475215, 8475220, 8475235, 8475728],
      'apb': [],
      'as': 0,
      'asog': 0,
      'desc': 'Jesse Puljujarvi Tip-In saved by Robin Lehner',
      'eventid': 59,
      'formalEventId': 'EDM59',
      'hoi': [8473468, 8474034, 8475660, 8477498, 8477934, 8479344],
      'hpb': [],
      'hs': 0,
      'hsog': 1,
      'localtime': '5:13 PM',
      'p1name': 'Jesse Puljujarvi',
      'p2name': 'Robin Lehner',
      'p3name': '',
      'period': 1,
      'pid': 8479344,
      'pid1': 8479344,
      'pid2': 8475215,
      'pid3': '',
      'playername': 'Jesse Puljujarvi',
      'strength': 701,
      'sweater': '98',
      'teamid': 22,
      'time': '01:32',
      'type': 'Shot',
      'xcoord': 81,
      'ycoord': 3}]}},
  'refreshInterval': 0}}

Tags: importjsondataasdesczacknormalizekyle
2条回答

如果只有一个游戏,这将创建所需的数据帧:

json_normalize(data['data']['game']['plays']['play'])

然后你只需要提取你感兴趣的列。

当结构变得复杂时,使用这个API可能是不直观的。 但关键是:json_normalize将json字段提取到表中。

就我而言:我有一张桌子

----------
|  fact  |  // each row is a json object {'a':a, 'b':b....}
----------

rrrrr = []
for index, row in data.iterrows():
    r1 = json_normalize(row['fact'])
    rrrrr.append(r1)
rr1 = pd.concat(rrrrr)

相关问题 更多 >