首页 > 解决方案 > 在 Windows 中使用 ValueError 运行 Python 脚本:列的长度必须与键的长度相同

问题描述

我可以在 MacOS10 中成功运行我的 Python3.7 脚本,但是在最新的 Windows10 Enterprise 中运行时,我的 Python 代码中显示如下错误,不确定是否有任何解决方案可以修复?

我的代码示例数据如下:

      A     B      C     D  Month
0     11    23     23   25    1
1     11    24     NA   26    2
2     12    20     18   18    1
3     12    NA     20   10    2

col=['Month','A','B','C']
df[col] = df[col].groupby(['A'], sort=False, as_index=False).ffill()

Windows环境下的错误信息如下:

 df[col] = df[col].groupby(['A'], sort=False, as_index=False).ffill()
 File "C:\Users\....\anaconda3\lib\site-packages\pandas\core\frame.py", line 3469, in __setitem__
 self._sentitem_array(key, value)

File "C:\Users\....\anaconda3\lib\site-packages\pandas\core\frame.py", line 3492, in _setitem_array
    raise ValueError('Columns must be same length as key')

ValueError: Columns must be same length as key

标签: python-3.xwindows

解决方案


推荐阅读