I have a dataframe df and I tried converting dataframe to json as below:
from flask import Flask
import pandas as pd
app=Flask(__name__)
@app.route('/graph')
def plot():
df=pd.DataFrame([['madrid', 10], ['venice', 20], ['las vegas', 40], ['brighton', 35]],
columns=['city', 'temp'])
jsondata=df.to_json(orient='records')
return jsondata
if __name__=='__main__':
app.run()
o/p as follows:
[{"city":"madrid","temp":10},{"city":"venice","temp":20},{"city":"las vegas","temp":40},{"city":"brighton","temp":35}]
and if I use orient='split' o/p is :
{"columns":["city","temp"],"index":[0,1,2,3],"data":[["madrid",10],["venice",20],["las vegas",40],["brighton",35]]}
I am looking for an output as:
{
"trace1":[{"city":["madrid",....],"temp":[10,20,...]}]
}
No idea to get for city and temp as above ,but tried for trace1 atleast as return {"trace1":jsondata} but this is giving me
{
"trace1": "{\"columns\":[\"city\",\"temp\"],\"index\":[0,1,2,3],\"data\":[[\"madrid\",10],[\"venice\",20],[\"las vegas\",40],[\"brighton\",35]]}"
}