I have this DataFrame:
A B C D
16 USA 15.5 10.8 4.7
15 Germany 17.7 12.3 5.3
I would like to create a JSON file that looks like so:
"data": [
["A", "B", "C", "D"],
["USA", "15.5", "10.8", "4.7"],
["Germany", "17.7", "12.3", "5.3],
]
to_dict() or to_json() do not seem to work.
This does work:
numpy_array = df.to_numpy()
column_headers = df.columns.values.tolist()
array_with_headers = numpy.vstack([column_headers, numpy_array])
json_object = {}
json_object["data"] = array_with_headers.tolist()
...is kind of complicated though. Any idea how I can achieve this without numpy? Thanks a lot!