Home > database >  How do you loop through api with list of parameters and store resulting calls in one dataframe
How do you loop through api with list of parameters and store resulting calls in one dataframe

Time:12-04

I'm trying to loop a list of match ids (LMID5) as parameters for api calls. I think I have the looping the API calls correct as it prints the urls but I'm struggling to store the results every time in the same dataframe.

The results of the API come through in JSON. Which I then normalise into a DF.

When just using one parameter to call api this is how I code it and create a df.

responsematchDetails = requests.get(url = matchDetails)
dfLM = pd.json_normalize(responseleagueMatches.json()['data'])

The issue is when trying to loop through a list of parameters and trying to store in one df. The below code is what I have wrote to try loop many calls to API using parameters from a list, but I'm struggling to store the data each time.

for i in list(LMID5):
    url = 'https://api.football-data-api.com/match?key=&match_id='   str(i)
    rm = requests.get(url)
    print(url)
    for match in pd.json_normalize(rm.json()["data"]):
            dfMatchDetails = dfMatchDetails.append({[match]
            }, ignore_index=True)

CodePudding user response:

Can you try this:

dfMatchDetails=pd.DataFrame()
for i in list(LMID5):
    url = 'https://api.football-data-api.com/match?key=&match_id='   str(i)
    rm = requests.get(url)
    print(url)
    dfMatchDetails=pd.concat([dfMatchDetails,pd.json_normalize(rm.json()['data'])])
  • Related