简体   繁体   中英

python loop down a column and append output to a dataframe

I am iterating down a column of product codes using df.iterrows():

The codes are then sent to an API and returns various details about my products. 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.

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)

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