使用pandas包处理excel的数据,将F列的数据加上引号后赋值到J列
pycharm环境和anaconda均可运行成功
使用Pyinstaller打包后出现下图的问题
个人判断是J列数据未初始化,为NaN无法操作,但不知道怎么解决(可能判断错误)
下图是错误提示 Failed to execute script '报表自动化4' due to unhandled exception: "None of [lndex([9],type='int32')] are in the [columns]"
excel文件示意图
局部代码:
# 修改列数据格式为string,后面加引号用
df[[5]] = df[[5]].astype(str)
# 此处是第33行,下一行代码报错
df[[9]] = df[[9]].astype(str)
df[[0,10]] = df[[0,10]].astype(str)
df[[11]] = df[[11]].astype(str)
【以下回答由 GPT 生成】
首先,根据你提供的信息,错误提示显示 Failed to execute script '报表自动化4' due to unhandled exception: "None of [lndex([9],type='int32')] are in the [columns]"
。根据错误提示,出现了df[[9]]
这一列不存在的问题。
解决这个问题的方法如下:
确认你的Excel文件中是否存在第9列,可以打开Excel文件检查一下。如果不存在第9列,那么将会出现列索引错误。
如果确认存在第9列,并且之前的代码在非打包状态下运行正常,那么说明问题可能是由于PyInstaller打包后无法正确处理pandas的数据类型导致的。
可以尝试修改代码,检查df
DataFrame 中是否存在第9列,如果不存在,就先创建一个空的第9列。可以使用df.insert()
方法来插入一个空的列,在df[[9]] = df[[9]].astype(str)
代码之前插入以下代码: python if 9 not in df.columns: df.insert(9, 9, '')
这样就可以确保df[[9]]
存在,继续进行后续操作。
修改后的代码如下所示: python if 9 not in df.columns: df.insert(9, 9, '') df[[5]] = df[[5]].astype(str) df[[9]] = df[[9]].astype(str) df[[0,10]] = df[[0,10]].astype(str) df[[11]] = df[[11]].astype(str)
如果以上方法都不能解决问题,还可以尝试以下方法:
确保使用的是最新版本的PyInstaller,可以尝试升级PyInstaller到最新版本,命令为 pip install --upgrade pyinstaller
。
使用其他打包工具,例如cx_Freeze、py2exe等,重新尝试打包并运行。
如果以上方法都不能解决问题,建议在相关的开发社区或者Python论坛中寻求帮助,提供尽可能详细的错误信息和代码示例,以及你使用的Python和pandas版本号。