Skip to content
Advertisement

Convert multi level Json File into a DataFrame PYTHON

How to convert this JSON file into a Pandas DataFrame. PYTHON

”’

URL="https://ressources.data.sncf.com/api/records/1.0/search/?dataset=sncf-gares-et-arrets-transilien-ile-de-france&q=&rows=9999&start=0&sort=libelle&facet=code_uic&facet=libelle_point_d_arret&facet=nom_gare&facet=commune&facet=zone_navigo&facet=gare_non_sncf"
req = requests.get(URL )
#text_data= req.text
json_dict= json.loads(text_data)
json_dict

”’

Advertisement

Answer

Use pd.json_normalize:

data = requests.get(URL).json()
df = pd.json_normalize(data['records'])
print(df)

# Output
                                         datasetid                                  recordid  ... geometry.type            geometry.coordinates
0    sncf-gares-et-arrets-transilien-ile-de-france  297f7655c89068015a0e90d1bcfdef6400c575b3  ...         Point  [2.41924456072, 48.7253771442]
1    sncf-gares-et-arrets-transilien-ile-de-france  42d1c6c90a2bbd0b0b2d86d35ff71b168ad5d92f  ...         Point  [2.09202131475, 48.9551807035]
2    sncf-gares-et-arrets-transilien-ile-de-france  dc6c79b461241cb64998f044d097a223c0cf43c1  ...         Point  [2.07739969014, 48.9700946335]
3    sncf-gares-et-arrets-transilien-ile-de-france  1be3bf1799e2a15402537e1609c147b73eb27d34  ...         Point  [2.56069879391, 49.0097447522]
4    sncf-gares-et-arrets-transilien-ile-de-france  39fea382bfe5bb656e8f87615e10151985770f48  ...         Point  [2.57154696503, 49.0040985205]
..                                             ...                                       ...  ...           ...                             ...
510  sncf-gares-et-arrets-transilien-ile-de-france  3dadd55b81461fce9450f2d0a79882717903c696  ...         Point   [2.40241090458, 48.800528137]
511  sncf-gares-et-arrets-transilien-ile-de-france  4c95f1133da62fae7e25d9a1125566a79f36e50f  ...         Point  [2.59909325048, 48.5149781204]
512  sncf-gares-et-arrets-transilien-ile-de-france  6ff2bd5aa5a583b66395c2974d4864edd3c3ffad  ...         Point   [1.62217099624, 48.274974514]
513  sncf-gares-et-arrets-transilien-ile-de-france  48499659fd75dc7bb8d95e3c5beae5c0076da341  ...         Point  [2.75278446885, 48.4305611079]
514  sncf-gares-et-arrets-transilien-ile-de-france  23f0787d9a956964169cba6703b0ea216cfdea3a  ...         Point  [2.48337131052, 48.7067634252]

[515 rows x 20 columns]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement