I have been looking at different methods to export pandas dataframes into json files but I am not sure how to include other string 'constants' into the JSON.
The purpose is to spit out a JSON file that can be read by chart.js .
The format of the pandas dataframe for the example is:
month order visit frequency
0 1월 171 2042 8.4
1 2월 72 475 15.2
2 3월 68 405 16.8
3 4월 84 991 8.5
4 5월 96 684 14.0
5 6월 58 576 10.1
6 7월 78 671 11.6
7 8월 67 576 11.6
8 9월 140 1168 12.0
9 10월 124 837 14.8
The format of the JSON required file is:
{
"labels": [
"1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월", "10월"
],
"datasets": [
{
"label": "order",
"data": [
171, 72, 68, 84, 96, 58, 78, 67, 140, 124
]
},
{
"label": "visit",
"data": [
2042, 475, 405, 991, 684, 576, 671, 576, 1168, 837
]
},
{
"label": "frequency",
"data": [
8.4, 15.2, 16.8, 8.5, 14, 10.1, 11.6, 11.6, 12, 14.8
]
}
]
}
i can export the pandas file as JSON using the inbuilt functions of pandas but i do not know how to separate the vectors and add the constant values seen above.
My purpose is a json format which can be used in chart.js
You could solve it like so:
df = df.set_index('month')
out = {'labels': df.index.tolist(), 'datasets': []}
for col in df.columns:
out['datasets'].append({
'label': col,
'data': df[col].values.tolist()
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.