【问题标题】:How to resolve "ValueError: Wrong number of items passed"?如何解决“ValueError:传递的项目数量错误”?
【发布时间】:2020-05-06 17:07:57
【问题描述】:

我无法使用此代码将数据导入 excel

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pandas as pd

driver = webdriver.Chrome()
mainurl = 'https://offerup.com/explore/sck/tx/austin/cars-trucks/'
driver.get(mainurl)
res = driver.execute_script("return document.documentElement.outerHTML")
page_soup = BeautifulSoup(res, 'html.parser')
# btn = driver.find_element_by_xpath('//*[@id="react-container"]/div/div[2]/div[2]/div[2]/div[3]/button').click()
records = []
for a in page_soup.find_all('span', class_='_nn5xny4 _y9ev9r'):
    title  = a.text
    print(title)
    records.append(title)
prices = []
for b in page_soup.find_all('span', class_='_s3g03e4'):
    price =  b.text
    print(price)
    prices.append(price)
location = []
for c in page_soup.find_all('span', class_='_19rx43s2'):
    loc = c.text
    print(loc)
    location.append(loc)

df = pd.DataFrame(records, prices, location)
print(df)
df.to_csv('trymenew.csv')

Traceback(最近一次调用最后一次):

  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\managers.py", line 1654, in create_block_manager_from_blocks
    make_block(values=blocks[0], placement=slice(0, len(axes[0])))
  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\blocks.py", line 3047, in make_block
    return klass(values, ndim=ndim, placement=placement)
  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\blocks.py", line 2595, in __init__
    super().__init__(values, ndim=ndim, placement=placement)
  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\blocks.py", line 124, in __init__
    raise ValueError(
ValueError: Wrong number of items passed 1, placement implies 44

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last):
  File "C:\Users\Saba\Desktop\cars_offerup.py", line 29, in <module>
    df = pd.DataFrame(records, prices, location)
  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\frame.py", line 488, in __init__
    mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)
  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\construction.py", line 210, in init_ndarray
    return create_block_manager_from_blocks(block_values, [columns, index])
  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\managers.py", line 1664, in create_block_manager_from_blocks
    construction_error(tot_items, blocks[0].shape[1:], axes, e)
  File "C:\Users\Saba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\managers.py", line 1694, in construction_error
    raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
ValueError: Shape of passed values is (44, 1), indices imply (44, 44)

我遇到了这种错误,你能帮我解决吗?

【问题讨论】:

  • 我首先将df = pd.DataFrame(records, prices, location) 更改为df = pd.DataFrame([records, prices, location])。由于我无法重现代码,因此我不确定您数据的其他形状。
  • 谢谢你成功了
  • 太棒了!我已将其发布为答案。

标签: python pandas csv beautifulsoup


【解决方案1】:

pd.DataFrame 我们可以 see in the documentation 期望数据作为第一个参数,但您将数据作为 3 个单独的参数传递。

为了解决这个问题,我们更改:
df = pd.DataFrame(records, prices, location) # Wrong

df = pd.DataFrame([records, prices, location]) # Correct

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 2016-04-09
    • 2021-07-13
    • 2021-04-08
    • 2021-07-05
    • 2020-01-30
    • 2020-05-06
    • 2021-08-10
    • 2020-07-02
    相关资源
    最近更新 更多