Search code examples
pythonpandasiteratorpython-requestsapi-design

How to iterate ids from dataframe into API to use to make calls with?


I have a dataframe that has 'id' column in it among 14 other columns.

df = 

id   column2
123  adfa
455  adfadf

I need to take each of the ids in the 'id' column and iteratively pass them into an API url, and store the result of the API output, ideally in a list.

The API URL looks like this:

r = requests.get(https://URL/v1/orders/{id}, headers = headers)

Each of the ids from the above df need to be passed into {id} and the result needs to be stored in the list []

I dont how to do this.

Thank you in advance.


Solution

  • Just use:

    ["https://URL/v1/orders/{id}".format(id=id) for id in df.id]
    

    For OP's edit:

    for id in df.id:
       r = requests.get("https://URL/v1/orders/{id}".format(id=id), headers = headers) 
       ...