Let's say I have a pandas df
like this:
| name | color of clothing | age |occupation|
| John | yellow | 34 | janitor |
| Carl | red | 27 | doctor |
| Claire | green | 33 | teacher |
| Lisa | blue | 21 | Student |
|........|...................| ....|..........|
I would like to transform it in a json format like this:
[
{ "name": "John,
"color of clothing": "yellow",
"age":"34",
"occupation": "janitor"
},
{ "name": "Carl",
"color of clothing": "red",
"age":"27",
"occupation": "doctor"
},
{ "name": "Claire",
"color of clothing": "green",
"age":"33",
"occupation": "teacher"
},
{ "name": "Lisa",
"color of clothing": "blue",
"age":"21",
"occupation": "student"
}
]
How can I do this? The nearest match I had was using df.to_json(orient="index")
but what I've got was a structure like this:
{"0":{ "name": "John,
"color of clothing": "yellow",
"age":"34",
"occupation": "janitor"
}
},
"1":{ "name": "Carl",
"color of clothing": "red",
"age":"27",
"occupation": "doctor"
}}
...
Would be really thankful for any help!
Use
df.to_dict('records')