0

我正在使用 json_normalize 函数,但它没有正确地将 JSON 导出到您可以在下面看到的 excel 文件中。 在此处输入图像描述

这是我的代码:

import requests
import pandas as pd

urls = [
    'https://www.cbr.nl/web/show?id=289168&langid=43&channel=json&cachetimeout=-1&elementHolder=289170&ssiObjectClassName=nl.gx.webmanager.cms.layout.PagePart&ssiObjectId=285674&contentid=11764&examtype=B', 
]
for url in urls:
    r = requests.get(url=url).json()
    print(r)
    jn = pd.json_normalize(r)
    df = pd.DataFrame(jn)
    df.to_excel('data.xlsx')
4

1 回答 1

1

您可以尝试将您的代码替换jn = pd.json_normalize(r)为以下代码,以便将列下的嵌套 json 列表扩展examInformation为单独的行:

使用.explode()+ pd.Series

jn = jn.drop('examInformation', axis=1).join(jn.explode('examInformation').apply(lambda x: pd.Series(x['examInformation']), axis=1))

或使用:.explode()+pd.DataFrame以加快执行速度:

jn_exp = jn['examInformation'].explode()
jn = jn.drop('examInformation', axis=1).join(pd.DataFrame(jn_exp.tolist(), index=jn_exp.index))

结果:

examInformation请参阅下面最右侧列中的扩展信息:

print(jn)

  vehicleCategory       type     id             name        lat       lon  examInformationAllLocations.allAttempts  examInformationAllLocations.successfulAllAttemptsPercentage contactInformation.streetName contactInformation.houseNumber contactInformation.houseNumberExtension contactInformation.zipCode contactInformation.city contactInformation.website contactInformation.email contactInformation.phone1 contactInformation.phone2 contactInformation.kvk contactInformation.drivingSchoolNumber contactInformation.tradeAssociations lessonTypes.Theorieopleidingen lessonTypes.Beroepsopleidingen lessonTypes.Bijzonderheden lessonTypes.Praktijkopleidingen              cbrLocation cbrLocationShortName                                     cbrLocationLink  locationSuccessfulPercentage  drivingSchoolSuccessfulPercentage  firstAttempts  successfulFirstAttemptsPercentage  retakeAttempts  successfulSecondAttemptsPercentage
0               B  rijschool  11764  Rijschool Baron  51.694322  5.287478                                       81                                                           43                  Amperestraat                             28                                                             5223CV        'S-HERTOGENBOSCH                                                                                          06 44 30 30 81           743056970000                                 1666U0                                                                  []                             []                         []                              []       Examencentrum Tiel                 Tiel     /nl/service/nl/artikel/examencentrum-tiel-1.htm                            57                                100              0                                  0               1                                 100
0               B  rijschool  11764  Rijschool Baron  51.694322  5.287478                                       81                                                           43                  Amperestraat                             28                                                             5223CV        'S-HERTOGENBOSCH                                                                                          06 44 30 30 81           743056970000                                 1666U0                                                                  []                             []                         []                              []  Examencentrum Den Bosch            Den Bosch  /nl/service/nl/artikel/examencentrum-den-bosch.htm                            54                                 42             30                                 43              50                                  42
于 2021-08-16T08:56:45.587 回答