Skip to content
Advertisement

how do I loop through each row and save the result in same row but in difference column?

I have a created a code where input variable is URL and it returns price, title, video, newURL

For instance:

a=product1.product_info(get_product_id('www.amazon.com'))
price = a.saleprice
title = a.producttitle
vid_url = a.provideo

And now, instead of copy and paste url over and over. I have created a CSV file like below and would like URL at each row to go through a loop and

  1. create a data (price, title)
  2. and save price, title data to same row with corresponding URL

enter image description here

So, I tried by using while loop as below:

raw_data = pd.read_csv(r"U:/test_csv_file.csv") #read csv file

numrow = len(raw_data) #to know when to stop while loop

url1 = 0
while url1 <numrow:
      url1 = url1 + 1
      df2 = df1.iloc[url1]
      product1 = amazon()       
      a=product1.product_info(get_product_id('df2'))
      price = a.saleprice
      title = a.producttitle
      vid_url = a.provideo
      if url1 == numrow:
            print("done")

However, I keep getting an error. I think there should be a better way of reading and wiriting a data at NaN elements but does not have any clue..

Advertisement

Answer

you can create the output dataframe based on a nested list, like this:

raw_data = pd.read_csv(r"U:/test_csv_file.csv") #read csv file

numrow = len(raw_data) #to know when to stop while loop

url1 = 0
data = []
while url1 <numrow:
      url1 = url1 + 1
      df2 = df1.iloc[url1]
      product1 = amazon()       
      a=product1.product_info(get_product_id('df2'))
      price = a.saleprice
      title = a.producttitle
      vid_url = a.provideo
      if url1 == numrow:
            print("done")
      data.append([price, title, vid_url])
cols = ['Price', 'Title', 'Vid_url']
df_out = pd.DataFrame(data=data, columns=cols)
Advertisement