简体   繁体   English

python向下循环一列并将输出追加到数据框

[英]python loop down a column and append output to a dataframe

I am iterating down a column of product codes using df.iterrows(): 我正在使用df.iterrows()迭代一列产品代码:

The codes are then sent to an API and returns various details about my products. 然后将代码发送到API,并返回有关我的产品的各种详细信息。 ie new and used sales prices. 即新的和二手的销售价格。

After each iteration I want to append the data to a dataframe. 每次迭代之后,我想将数据追加到数据框。 I should be seeing 100s of rows of data in my dataframe but instead all I receive is a single row of data for the final product code. 我应该在数据框中看到100行数据,但是我收到的只是最终产品代码的一行数据。

I think I need to create a second nested loop takes my output at each iteration and appends it to a dataframe, but I'm not sure where to begin. 我想我需要创建第二个嵌套循环,每次迭代都将我的输出添加到数据帧中,但是我不确定从哪里开始。 My code is below. 我的代码如下。 Any help would be appreciated. 任何帮助,将不胜感激。

import numpy as np
import pandas as pd

accesskey = 'xxxx'
api = keepaAPI.API(accesskey)

df = pd.read_excel('C:/Users/xxxx.xlsx',
                  sheet_name = 'abebooks',
                  header = 0,
                  index_col = None,
                  usecols = "A:P",
                  convert_float = True)

for index, row in df.iterrows():
products = api.ProductQuery(row['xxx'])
product = products[0]

newprice = products[0]['data']['NEW']
newpricetime = products[0]['data']['NEW_time']
usedprice = products[0]['data']['USED']
usedpricetime = products[0]['data']['USED_time']
bsr = products[0]['data']['SALES']
bsrtime = products[0]['data']['SALES_time']

df = pd.DataFrame([[products[0]['title'], 
products[0]['asin'],newprice[-1], usedprice[-1], bsr[-1], 
products[0]['binding']]])

df2 = pd.DataFrame([], columns=list(["title", "Asin", 
"New price", "Used price", "BSR", "Binding"]))

df.append(df2, ignore_index=True)

got there in the end, created a list of dataframes, and concat them at the end of the loop: 最后到达那里,创建一个数据帧列表,并在循环结束时连接它们:

dfa_list = []

import numpy as np
import pandas as pd

accesskey = 'xxxx'
api = keepaAPI.API(accesskey)

df = pd.read_excel('C:/Users/xxxx.xlsx',
              sheet_name = 'abebooks',
              header = 0,
              index_col = None,
              usecols = "A:P",
              convert_float = True)

for index, row in df.iterrows():
products = api.ProductQuery(row['xxx'])
product = products[0]

newprice = products[0]['data']['NEW']
newpricetime = products[0]['data']['NEW_time']
usedprice = products[0]['data']['USED']
usedpricetime = products[0]['data']['USED_time']
bsr = products[0]['data']['SALES']
bsrtime = products[0]['data']['SALES_time']

df = pd.DataFrame([[products[0]['title'], 
products[0]['asin'],newprice[-1], usedprice[-1], bsr[-1], 
products[0]['binding']]])

df2 = pd.DataFrame([], columns=list(["title", "Asin", 
"New price", "Used price", "BSR", "Binding"]))


dfa_list.append(df2)

it_df = pd.concat(dfa_list)

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

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