I’ve used most of methods to load a json and the results I got didn’t meet my needs. When I load the file with pandas read_json, I get a dataframe with the index and all fields in a single line, as a string. Here’s the two calls I do that doesn’t work for me:
import json
import pandas as pd
#Open and read the JSON file
with open('faturamento.json') as f:
d = json.load(f)
print(d)
fat = pd.read_json("faturamento.json")
And faturamento.json is:
{
"faturamento": [
{
"day": "2024-09-01",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-02",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-03",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-04",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-05",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-06",
"fat": "1000",
"business_day": "FALSE"
},
{
"day": "2024-09-07",
"fat": " 0",
"business_day": "FALSE"
},
{
"day": "2024-09-08",
"fat": "1500",
"business_day": "TRUE"
},
{
"day": "2024-09-09",
"fat": "1500",
"business_day": "TRUE"
},
{
"day": "2024-09-10",
"fat": "1500",
"business_day": "TRUE"
},
{
"day": "2024-09-11",
"fat": "1500",
"business_day": "TRUE"
},
{
"day": "2024-09-12",
"fat": "1500",
"business_day": "TRUE"
},
{
"day": "2024-09-13",
"fat": " 0",
"business_day": "FALSE"
},
{
"day": "2024-09-14",
"fat": " 0",
"business_day": "FALSE"
},
{
"day": "2024-09-15",
"fat": "2500",
"business_day": "TRUE"
},
{
"day": "2024-09-16",
"fat": "2500",
"business_day": "TRUE"
},
{
"day": "2024-09-17",
"fat": "2500",
"business_day": "TRUE"
},
{
"day": "2024-09-18",
"fat": "2500",
"business_day": "TRUE"
},
{
"day": "2024-09-19",
"fat": "2500",
"business_day": "TRUE"
},
{
"day": "2024-09-20",
"fat": " 0",
"business_day": "FALSE"
},
{
"day": "2024-09-21",
"fat": " 0",
"business_day": "FALSE"
},
{
"day": "2024-09-22",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-23",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-24",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-25",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-26",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-27",
"fat": " 500",
"business_day": "TRUE"
},
{
"day": "2024-09-28",
"fat": "1500",
"business_day": "FALSE"
},
{
"day": "2024-09-29",
"fat": " 0",
"business_day": "FALSE"
},
{
"day": "2024-09-30",
"fat": "3500",
"business_day": "TRUE"
}
]
}
I thought that read_json would do the trick. How can I really have a dataframe with three columns: day, fat, business_day?
2
Answers
you need to use pd.json_normalize:
Output:
Two options:
or
Both will output: