简体   繁体   中英

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.

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

df = df.append(data)

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