ValueError arrays must all be same length ValueError arrays must all be same length

0 votes
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import pandas as pd
import xlsxwriter

my_url = "https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2495737.m570.l1313.TR11.TRC2.A0.H1.XSamsung+Galaxy+.TRS1&_nkw=Samsung+Galaxy+&_sacat=0"

uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")

containers = page_soup.findAll("div", {"class": "s-item__info clearfix"})

product = []
price = []
condition = []

# filename = "ebay2021.csv"
# f = open(filename, "w", encoding='utf-8')
# headers = "product_name, product_price, product_condition\n"
# f.write(headers)

for container in containers:
    brand = container.findAll('h3', {'class': 's-item__title'})
    product_name = brand[0].text

    price = container.findAll('span', {'class': 's-item__price'})
    product_price = price[0].text
    #
    condition = container.findAll("span", {"class": "SECONDARY_INFO"})
    product_condition = condition[0].text

    product.append(product_name)
    price.append(product_price)
    condition.append(product_condition)

    df = pd.DataFrame({'Product Name': product, 'Price': price, 'Rating': condition})
    df.to_csv('products.csv', index=False, encoding='utf-8')
Mar 21, 2020 in Python by Edureka
• 140 points

edited Jun 25, 2020 by MD 24,028 views

Hey, You can do this to avoid the error:

a = {'Product Name': product, 'Price': price, 'Rating': condition}
df = pd.DataFrame.from_dict(a, orient='index')
df.transpose()

Yes, this worked! make sure to add the last line otherwise it will not store data into CSV.

Thanks :-)

a = {

    "Price": prices,

    "Property Type" : types,

    "Infromation": descriptions,

    "Location " : addresses,

    "Features" : features

}

df = pd.DataFrame.from_dict(a, orient='index')

df.transpose()

df.to_csv('website3.csv', index=False,header=True, encoding='utf-8')

1 answer to this question.

0 votes

Hi, 

You need to specify index for your DataFrame. Without an index, Pandas will not you to convert your dictionary to DataFrame. You can modify your code as given below.

a = {'Product Name': product, 'Price': price, 'Rating': condition}
df = pd.DataFrame.from_dict(a, orient='index')
df.transpose()
df.to_csv('website3.csv', index=False,header=True, encoding='utf-8')
answered Jun 25, 2020 by MD
• 95,360 points

Related Questions In Python

0 votes
1 answer

ValueError: arrays must all be same length

Hello, For your query you can refer this:https://www.programmersought.com/article/5321489391/ Hope ...READ MORE

answered Sep 21, 2020 in Python by Niroj
• 82,740 points
3,610 views
0 votes
0 answers
0 votes
1 answer

In LightFM does the training set need to be the same size as the test set?

I would change the wording. Often in ML, ...READ MORE

answered Sep 21, 2018 in Python by charlie_brown
• 7,710 points
200 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,912 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
2,140 views
0 votes
1 answer

TypeError: list indices must be integers or slices, not str

Hi@akhtar, As we know that the index of ...READ MORE

answered Oct 15, 2020 in Python by MD
• 95,360 points
12,604 views
0 votes
1 answer

ValueError: could not broadcast input array from shape (360,270,3) into shape (360,280,3)

Hi@akhtar, In the above error it shows could not ...READ MORE

answered Apr 9, 2020 in Python by MD
• 95,360 points
9,029 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP