【发布时间】: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_sql的con 参数。