Python Forum
Converting a json file to a dataframe with rows and columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting a json file to a dataframe with rows and columns
#13
(Jan-29-2023, 11:02 AM)eyavuz21 Wrote: Thanks so much for your help. I have tried your code above - it gives me an empty dataframe.

When I try the following code:
You mess the json file to a string when doing that,most use json.load(file) and not file.read()
You post complete raw json data,or working smample of it it's big.

Here a comlete example that help if you look at undertand how it works.
Use eg jsoncrack to see the structure better and that is vaild json file.
game.json
Output:
{ "results": [ { "leagues": [ { "matches": [ { "league": "Premier League", "teams": "Liverpool - Chelsea", "score": [ "3:0" ] }, { "league": "Premier League", "teams": "Man Utd - Arsenal", "score": [ "2:1" ] } ] }, { "matches": [ { "league": "La Liga", "teams": "Atletico Madrid - Villareal", "score": [ "0:2" ] } ] } ] } ] }
I want this result of game.json as a DataFrame.
Output:
league teams score 0 Premier League Liverpool - Chelsea [3:0] 1 Premier League Man Utd - Arsenal [2:1]
Then i would use json_normalize like this in code under.
import pandas as pd
import json

with open('game.json') as file:
    json_data = json.load(file)

df = pd.json_normalize(json_data['results'][0]['leagues'][0], record_path='matches')
Look at result.
>>> df
           league                teams  score
0  Premier League  Liverpool - Chelsea  [3:0]
1  Premier League    Man Utd - Arsenal  [2:1]
Reply


Messages In This Thread
RE: Converting a json file to a dataframe with rows and columns - by snippsat - Jan-29-2023, 01:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 755 Apr-24-2025, 01:05 PM
Last Post: snippsat
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,654 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,231 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  docx file to pandas dataframe/excel iitip92 1 3,129 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  encrypt data in json file help jacksfrustration 1 2,485 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Converting column of values into muliple columns of counts highland44 0 975 Feb-01-2024, 12:48 AM
Last Post: highland44
  Converting .txt to .csv file SunWers 21 22,935 Jan-20-2024, 10:03 AM
Last Post: Larz60+
  Create Choices from .ods file columns cspower 3 1,977 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 2,240 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Create csv file with 4 columns for process mining thomaskissas33 3 1,973 Nov-06-2023, 09:36 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020