I have a created a code where input variable is URL and it returns price, title, video, newURL
For instance:
JavaScript
x
5
1
a=product1.product_info(get_product_id('www.amazon.com'))
2
price = a.saleprice
3
title = a.producttitle
4
vid_url = a.provideo
5
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
- create a data (price, title)
- and save price, title data to same row with corresponding URL
So, I tried by using while loop as below:
JavaScript
1
16
16
1
raw_data = pd.read_csv(r"U:/test_csv_file.csv") #read csv file
2
3
numrow = len(raw_data) #to know when to stop while loop
4
5
url1 = 0
6
while url1 <numrow:
7
url1 = url1 + 1
8
df2 = df1.iloc[url1]
9
product1 = amazon()
10
a=product1.product_info(get_product_id('df2'))
11
price = a.saleprice
12
title = a.producttitle
13
vid_url = a.provideo
14
if url1 == numrow:
15
print("done")
16
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:
JavaScript
1
20
20
1
raw_data = pd.read_csv(r"U:/test_csv_file.csv") #read csv file
2
3
numrow = len(raw_data) #to know when to stop while loop
4
5
url1 = 0
6
data = []
7
while url1 <numrow:
8
url1 = url1 + 1
9
df2 = df1.iloc[url1]
10
product1 = amazon()
11
a=product1.product_info(get_product_id('df2'))
12
price = a.saleprice
13
title = a.producttitle
14
vid_url = a.provideo
15
if url1 == numrow:
16
print("done")
17
data.append([price, title, vid_url])
18
cols = ['Price', 'Title', 'Vid_url']
19
df_out = pd.DataFrame(data=data, columns=cols)
20