Search code examples
pythonjsonpython-3.xhttpget

Append to a DataFrame within a for loop


I have data in a csv file, where I want to execute a HTTP GET request for every row in the csv and store the results of the request in a DataFrame.

Here's what I'm working with so far:

with open('input.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
df = pd.DataFrame()
for row in csv_reader:
    result = requests.get(BASEURL+row['ID']+"&access_token="+TOKEN).json()
    data = pd.DataFrame(result)
    df.append(data)

However, this doesn't seem to be appending to the df?

Note the json response will always return id, first_name, last_name key-value pairs.


Solution

  • The append operation returns a new dataframe with the appended data. Change the last line to:

    df = df.append(data)