Pandas读取嵌套json

2024-05-19 12:24:20 发布

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

我很好奇如何使用pandas读取以下结构的嵌套json:

{
    "number": "",
    "date": "01.10.2016",
    "name": "R 3932",
    "locations": [
        {
            "depTimeDiffMin": "0",
            "name": "Spital am Pyhrn Bahnhof",
            "arrTime": "",
            "depTime": "06:32",
            "platform": "2",
            "stationIdx": "0",
            "arrTimeDiffMin": "",
            "track": "R 3932"
        },
        {
            "depTimeDiffMin": "0",
            "name": "Windischgarsten Bahnhof",
            "arrTime": "06:37",
            "depTime": "06:40",
            "platform": "2",
            "stationIdx": "1",
            "arrTimeDiffMin": "1",
            "track": ""
        },
        {
            "depTimeDiffMin": "",
            "name": "Linz/Donau Hbf",
            "arrTime": "08:24",
            "depTime": "",
            "platform": "1A-B",
            "stationIdx": "22",
            "arrTimeDiffMin": "1",
            "track": ""
        }
    ]
}

这里保持数组为json。我宁愿把它扩展成列。

pd.read_json("/myJson.json", orient='records')

编辑

谢谢你的第一个回答。 我应该完善我的问题: 数组中嵌套属性的展平不是必需的。 只需[A,B,C]连接df.locations['name']就可以了。

我的文件包含多个JSON对象(每行1个),我希望保留number、date、name和locations列。不过,我需要加入这些地点。

allLocations = ""
isFirst = True
for location in result.locations:
    if isFirst:
        isFirst = False
        allLocations = location['name']
    else:
        allLocations += "; " + location['name']
allLocations

我在这方面的做法似乎没有效率/熊猫式。


Tags: namejsonnumberdatelocationtrackplatformlocations
1条回答
网友
1楼 · 发布于 2024-05-19 12:24:20

您可以使用^{}

import json
from pandas.io.json import json_normalize    

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

df = json_normalize(data, 'locations', ['date', 'number', 'name'], 
                    record_prefix='locations_')
print (df)
  locations_arrTime locations_arrTimeDiffMin locations_depTime  \
0                                                        06:32   
1             06:37                        1             06:40   
2             08:24                        1                     

  locations_depTimeDiffMin           locations_name locations_platform  \
0                        0  Spital am Pyhrn Bahnhof                  2   
1                        0  Windischgarsten Bahnhof                  2   
2                                    Linz/Donau Hbf               1A-B   

  locations_stationIdx locations_track number    name        date  
0                    0          R 3932         R 3932  01.10.2016  
1                    1                         R 3932  01.10.2016  
2                   22                         R 3932  01.10.2016 

编辑:

您可以使用^{}通过DataFrame构造函数解析name,最后一个^{}使用applyjoin

df = pd.read_json("myJson.json")
df.locations = pd.DataFrame(df.locations.values.tolist())['name']
df = df.groupby(['date','name','number'])['locations'].apply(','.join).reset_index()
print (df)
        date    name number                                          locations
0 2016-01-10  R 3932         Spital am Pyhrn Bahnhof,Windischgarsten Bahnho... 

相关问题 更多 >