简体   繁体   English

在for循环中附加到DataFrame

[英]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. 我有一个csv文件中的数据,我想在该文件中为csv中的每一行执行一个HTTP GET请求,并将请求的结果存储在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? 但是,这似乎没有追加到df吗?

Note the json response will always return id, first_name, last_name key-value pairs. 请注意,json响应将始终返回id,first_name,last_name键值对。

The append operation returns a new dataframe with the appended data. 追加操作返回带有追加数据的新数据帧。 Change the last line to: 将最后一行更改为:

df = df.append(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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