Apr-02-2025, 09:40 PM
(This post was last modified: Apr-02-2025, 09:41 PM by deanhystad.)
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.
Your next post better have some code you wrote and error messages or sample input/output.
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,60Notice 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.