我是一名新手程序员,尝试将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。请尽可能提供帮助!
如果有帮助,我还将pyodbc与驱动程序ODBC Driver 17 for SQL Server
和Microsoft SQL Server 19一起使用。
错误代码:
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)
答案 0 :(得分:0)
您为数据框提供了第一个col的列表,但是您启动了4。所以至少这就是我理解错误的方式。
尝试:
df = pd.DataFrame({"Num": listNumValues, "Str": listStrValues, "Float": listFloatVlálues, "ID": listIDValues})
如果您提供的数据集看起来像来自sql的数据,那么我可以帮助您进行转换。