I have a csv file I am trying convert to json with pandas.
file.csv:
Symbol,Name,Sector
MMM,3M Company,Industrials
AOS,A.O. Smith Corp,Industrials
ABT,Abbott Laboratories,Health Care
python code:
import json
import pandas as pd
df = pd.read_csv(file.csv, sep=",")
with open(file.json, 'w') as f:
f.write(df.to_json(orient='records', lines=True, indent=4))
This results with
file.json:
{
"Symbol":"MMM",
"Name":"3M Company",
"Sector":"Industrials"
}
{
"Symbol":"AOS",
"Name":"A.O. Smith Corp",
"Sector":"Industrials"
}
{
"Symbol":"ABT",
"Name":"Abbott Laboratories",
"Sector":"Health Care"
}
As you can see, it needs commas after the first 2 closing curly braces, as well as the carriage returns deleted.
Also, I would like to nest the data inside a labeled bracket, add some more data at the bottom, and then nest all that inside a pair of curly braces for proper json form, resulting in this:
{
"SP500": [
{
"Symbol":"MMM",
"Name":"3M Company",
"Sector":"Industrials"
},
{
"Symbol":"AOS",
"Name":"A.O. Smith Corp",
"Sector":"Industrials"
},
{
"Symbol":"ABT",
"Name":"Abbott Laboratories",
"Sector":"Health Care"
}
],
"moreData": "datadatadata"
}
I could probably hack together something to basically do this all explicitly, editing the json file line by line, but I wondered if there are certain parameters for the methods I’m using, or other methods that would do this more elegantly?