简体   繁体   中英

How to send a pandas dataframe to a flask_restful api?

Right now I am sending one item at a time to a flask_restful api, and attaching the results to a dataframe.

The api would be much faster if it could take a whole dataframe rather than do it row by row.

Is this possible?

Here is a very basic mockup of what I am doing.

This api takes the person's name, and returns a sentence: "Your name is..."

from flask import Flask
from flask_restful import Resource, Api, reqparse

app = Flask(__name__)
api = Api(app)

class my_api(Resource):


    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('name', type = str, default='')
        
        super(my_api, self).__init__()

    def get(self):

        args = self.reqparse.parse_args()
        name =  args['name']
        
        return f'Your name is {name}'

api.add_resource(my_api, '/')

if __name__ == '__main__':
    app.run(debug=True)

Now I'll use the api on my dataframe:

df = pd.DataFrame({'name':['John','Jim','Mary','Michael']})
print(df)

      name
0     John
1      Jim
2     Mary
3  Michael


# list to save api results
results = []

# send each name in the column to the api
for name in df['name']:

    # api call
    api_data = send name to api
    
    results.append(api_data)

# attach results to dataframe
df['results'] = results

So, the way my actual flask api is set up, it would be much faster if I got the full dataframe rather than row by row. Also, the dataframe is bigger than the mockup above. There are more columns and rows.

Any advice?

You could convert your dataframe into JSON and send it to the API. You'll have to configure your API to accept parse the JSON. This way you can send the complete data all at once. Also using snake_case for class names is not recommended

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM