Python 如何从pandas dataframe组织JSON数据
我不知道如何正确组织从pandas数据帧创建的JSON数据。这是我的代码:Python 如何从pandas dataframe组织JSON数据,python,json,pandas,Python,Json,Pandas,我不知道如何正确组织从pandas数据帧创建的JSON数据。这是我的代码: with open (spreadsheetName, 'rb') as spreadsheet: newSheet = spreadsheet.read() newSheet = pd.read_excel(newSheet) exportSheet = newSheet.to_json('file.json', orient = 'index') 我希望JSON数据看起来像 { "cars
with open (spreadsheetName, 'rb') as spreadsheet:
newSheet = spreadsheet.read()
newSheet = pd.read_excel(newSheet)
exportSheet = newSheet.to_json('file.json', orient = 'index')
我希望JSON数据看起来像
{
"cars": [
{
"Model": "Camry",
"Year": "2015"
},
{
"Model": "Model S",
"Year": "2018"
}
]
}
但是我从我的代码中得到了一行JSON数据。关于如何使每一行成为一个JSON“对象”以及列标题中的键和值(如model和year),有什么想法吗?在
to_JSON
函数中将缩进参数设置为所需值
exportSheet = newSheet.to_json('file.json', orient='index', indent=4)