Return Pandas dataframe as JSONP response in Python Flask Return Pandas dataframe as JSONP response in Python Flask flask flask

Return Pandas dataframe as JSONP response in Python Flask


This is my solution to convert a Pandas dataframe to JSONP an return it in Flask:

from flask_jsonpify import jsonpifydf_list = df.values.tolist()JSONP_data = jsonpify(df_list)return JSONP_data

Depending on how you need the dataframe converted, you might need to create the list in a different way. For example like this:

df_list = merged.values.T.tolist() 

Or like this:

df_list = list(df.values.flatten())

Thanks goes to the user @Barmer


With this method, columns of dataframe will be the keys and series of dataframe will be the values.

data_dict = dict()for col in dataframe.columns:    data_dict[col] = dataframe[col].values.totlist()return jsonify(data_dict)