I have the panda dataframe df
below;
File Hour
Name1 F1 1
Name2 F1 2
I want to convert it into json that looks like this;
{
"Name1": {
"File": "F1",
"Hour": "1"
},
"Name2": {
"File": "F2",
"Hour": "2"
}
}
How can I use df.to_json()
to do this? I am welcomed to using other methods.
I am using python v3.9
CodePudding user response:
As hinted here, you can use to_json
this way:
df.to_json(orient='index')
or
df.T.to_json()
output:
'{"Name1":{"File":"F1","Hour":1},"Name2":{"File":"F1","Hour":2}}'