Python Forum
pandas.json_normalize question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas.json_normalize question
#4
Sounds like you know all you need to proceed. Time for you to start writing some code. Trial and error is important at the early stages of learning.

I wrote this to see what happens when I use pandas to load the json file I created earlier.
import pandas as pd

df = pd.read_json("data.json")
print(df)
Output:
id name fitness 0 1.0 Cole Volk {'height': 130, 'weight': 60} 1 NaN Mark Reg {'height': 130, 'weight': 60} 2 2.0 Faye Raker {'height': 130, 'weight': 60}
Notice that fitness is a dictionary, not columns. If I try to write the pandas dataframe to a csv format file:
import pandas as pd

df = pd.read_json("data.json")
df.to_csv("data.csv")
I get this in data.csv
Output:
,id,name,fitness 0,1.0,Cole Volk,"{'height': 130, 'weight': 60}" 1,,Mark Reg,"{'height': 130, 'weight': 60}" 2,2.0,Faye Raker,"{'height': 130, 'weight': 60}"
Not really a csv format file. That is why the dataframe had to be normalized.
import json
import pandas as pd


with open("data.json", "r") as file:
    data = json.load(file)
df = pd.json_normalize(data, max_level=1)
print(df)
df.to_csv("data.csv")
output
Output:
id name fitness.height fitness.weight 0 1.0 Cole Volk 130 60 1 NaN Mark Reg 130 60 2 2.0 Faye Raker 130 60
data.csv
,id,name,fitness.height,fitness.weight
0,1.0,Cole Volk,130,60
1,,Mark Reg,130,60
2,2.0,Faye Raker,130,60
Notice in your original post that the call to pd.json_normalize() takes data, a list of dictionaries, not an existing dataframe as input. Read the json file to get the list of dictionaries. Use json_normalize(data) to create a dataframe that expands a dictionary into columns.

Your next post better have some code you wrote and error messages or sample input/output.
elsvieta likes this post
Reply


Messages In This Thread
pandas.json_normalize question - by elsvieta - Apr-02-2025, 05:49 PM
RE: pandas.json_normalize question - by elsvieta - Apr-02-2025, 08:42 PM
RE: pandas.json_normalize question - by deanhystad - Apr-02-2025, 09:01 PM
RE: pandas.json_normalize question - by deanhystad - Apr-02-2025, 09:40 PM
RE: pandas.json_normalize question - by elsvieta - Apr-03-2025, 07:36 PM
RE: pandas.json_normalize question - by deanhystad - Apr-03-2025, 09:39 PM
RE: pandas.json_normalize question - by Pedroski55 - Apr-04-2025, 03:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas and MongoDB question Majority390 1 1,542 Dec-23-2024, 02:41 AM
Last Post: sakshi009
  pandas df inside a df question mbaker_wv 4 2,308 Dec-25-2022, 01:11 AM
Last Post: mbaker_wv
  Pandas usecols question rsearing 1 2,019 Aug-20-2022, 10:10 PM
Last Post: jefsummers
  Simple pandas question mcva 4 3,849 Dec-17-2021, 04:47 PM
Last Post: mcva
  Pandas question new2datasci 0 2,564 Jan-10-2021, 01:29 AM
Last Post: new2datasci
  Pandas merge question smw10c 2 6,628 Jul-02-2020, 06:56 PM
Last Post: hussainmujtaba
  Counting Criteria in Pandas Question Koenig 1 2,817 Sep-30-2019, 05:16 AM
Last Post: perfringo
  Function question using Pandas smw10c 7 8,892 Feb-12-2019, 06:52 PM
Last Post: Nathandsn
  Simple pandas dataframe question popohoma 1 4,520 Jan-03-2019, 05:00 PM
Last Post: ashlardev
  question on pandas datareader kit12_31 3 10,785 Feb-05-2018, 11:55 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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