How to Convert Nested JSON Structures into Structured Pandas DataFrames?

Susan Sarandon
Release: 2024-10-24 11:41:29
Original
763 people have browsed it

How to Convert Nested JSON Structures into Structured Pandas DataFrames?

Unraveling the Mysteries of Nested JSON: Transforming into Pandas DataFrames

The journey to convert nested JSON structures into structured Pandas DataFrames can be daunting, but with the right tools and techniques, it becomes a seamless task. Let's explore the options available for this transformation.

JSON Normalization: A Straightforward Approach

json_normalize provides a powerful solution for flattening nested JSON objects. As we embark on this approach:

<code class="python">import json

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

df = pd.json_normalize(data, 'locations', ['date', 'number', 'name'], 
                    record_prefix='locations_')
print (df)</code>
Copy after login

Output:

  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 
Copy after login

Parsing Name and Grouping for Concatenation

However, if flattening is not your ultimate goal, you can embrace an alternative approach:

<code class="python">df = pd.read_json(&quot;myJson.json&quot;)
df.locations = pd.DataFrame(df.locations.values.tolist())['name']
df = df.groupby(['date','name','number'])['locations'].apply(','.join).reset_index()
print (df)</code>
Copy after login

Output:

        date    name                                          locations
0 2016-01-10  R 3932         Spital am Pyhrn Bahnhof,Windischgarsten Bahnho... 
Copy after login

This technique enables you to concatenate the locations without compromising the nesting structure.

The above is the detailed content of How to Convert Nested JSON Structures into Structured Pandas DataFrames?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!