Reputation: 157
Using Flask and pandas, I want to add this variable:
jsonized = file_data.to_json(orient='values') #file_data is a result from pandas.read_csv
as a value to a JSON property
json_example = {
'data': jsonized,
'result': 0,
'message': message
}
But if I do this, and return the data to the front-end (React), the property turns out as a string.
data:"["some data in what's supposed to be a list"]"
If I return just the jsonized variable to the front-end, it will format properly as a list.
[["data"], ["data"], ["more data"]]
What I would like to return to the front-end is this:
{
data: [['data'], ['data'], ['more data']],
result: 0
message: 'data has been returned',
}
How can I achieve this?
Upvotes: 1
Views: 173
Reputation: 4130
If you need to convert every row to list,for example I created this dummy dataframe.
file_data= pd.DataFrame(np.random.randint(0,100,size=(5, 4)), columns=list('ABCD'))
A B C D
0 25 25 43 49
1 29 12 14 67
2 66 96 95 16
3 91 6 46 32
4 48 10 27 62
use dataframe values and convert it to list using tolist()
jsonized = df.values.tolist()
output
[[25, 25, 43, 49],
[29, 12, 14, 67],
[66, 96, 95, 16],
[91, 6, 46, 32],
[48, 10, 27, 62]]
Upvotes: 1