I have a csv formatted like:
Column1,Column2,Column3
data1,data2,data3
data1,data2,data3
And I was able to convert it to json in this format:
[
{
"Column1": "data1",
"Column2": "data2",
"Column3": "data3",
},
{
"Column1": "data1",
"Column2": "data2",
"Column3": "data3",
},
]
with this code:
import pandas as pd
import json
df = pd.read_csv(r'ReactStepsSubsteps.csv', encoding='windows-1254')
newList = []
for index, item in df.iterrows():
newDict = {}
for key in item.keys():
newDict[key] = item[key]
newList.append(newDict)
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(newList, f, ensure_ascii=False, indent=4)
Is there a way to do this exclusively with helper functions from pandas?