【问题标题】:Pandas DataFrame throwing ValueErrorPandas DataFrame 抛出 ValueError
【发布时间】:2019-12-23 16:52:28
【问题描述】:

我是一个新手程序员,试图将 sql 转换为 excel 作为一个辅助项目。它使用 Pandas DataFrame。进入它的命令是 df = pd.DataFrame(list(data), columns=columns) 进入这个的变量是

data: [('300       ', None, '300.05    ', 'test      '), ('300       ', None, None, None), (None, None, '300.05    ', None), (None, None, None, 'ID::4     ')]
columns: ['Num', 'Str', 'Float', 'ID']

我不知道是什么导致了 ValueError。请尽可能提供帮助!

如果有帮助,我还在使用带有驱动程序 ODBC Driver 17 for SQL Server 和 Microsoft SQL Server 19 的 pyodbc。

错误代码:

Traceback (most recent call last):
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\internals\managers.py", line 1678, in create_block_manager_from_blocks
    make_block(values=blocks[0], placement=slice(0, len(axes[0])))
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\internals\blocks.py", line 3284, in make_block
    return klass(values, ndim=ndim, placement=placement)
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\internals\blocks.py", line 2792, in __init__
    super().__init__(values, ndim=ndim, placement=placement)
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\internals\blocks.py", line 128, in __init__
    "{mgr}".format(val=len(self.values), mgr=len(self.mgr_locs))
ValueError: Wrong number of items passed 1, placement implies 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "G:\Py\pyxl\pyxl.py", line 18, in <module>
    df = pd.DataFrame(list(data), None, columns, None, None)
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\frame.py", line 464, in __init__
    mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\internals\construction.py", line 213, in init_ndarray
    return create_block_manager_from_blocks(block_values, [columns, index])
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\internals\managers.py", line 1688, in create_block_manager_from_blocks
    construction_error(tot_items, blocks[0].shape[1:], axes, e)
  File "C:\Users\joshu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\internals\managers.py", line 1719, in construction_error
    "Shape of passed values is {0}, indices imply {1}".format(passed, implied)
ValueError: Shape of passed values is (4, 1), indices imply (4, 4)

【问题讨论】:

  • 请注意:pandas 支持使用read_sql 直接查询。
  • 是否可以使用 read_sql 建立 pyodbc 连接? @Parfait
  • 是的。只需将pyodbc 连接传递到read_sqlcon 参数。

标签: python sql excel pyodbc


【解决方案1】:

您为数据框提供了第一个 col 的列表,但您启动了 4。所以至少我是这样理解错误的。

试试:

df = pd.DataFrame({"Num": listNumValues, "Str": listStrValues, "Float": listFloatVlálues, "ID": listIDValues})

如果您提供的数据集看起来像您的 sql 数据,那么我可以帮助您转换它。

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 2020-03-29
    • 2015-10-11
    • 2022-08-11
    • 1970-01-01
    • 2021-04-04
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多