天宫鹤

成功解决pandas报AttributeError: DataFrame object has no attribute ix错误

在实际操作中有时候需要把采集的数据或者分析完的数据保存为excel中,列名按照执行排序,一开始我采用DataFrame的ix方式去实现是可以达到预期的,不过最近发现好像该方法函数被移除了,运行会抛出以下错误:

Traceback (most recent call last):
File "test.py", line 149, in <module>
test()
File "test.py", line 143, in test
result_data, cols = add_excel(sheet_list, cols, excels, self.path)
File "test.py", line 47, in add_excel
DataFrame = DataFrame.ix[:, cols]
File "E:\project\test\venv\lib\site-packages\pandas\core\generic.py", line 5273, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'ix'

根本原因
由于安装了较高版本的pandas,官方移除了一些不推荐使用的方法函数。

解决方式

根据官方说明,ix已被移除,可用.loc替代。

具体解决办法

将\Lib\site-packages\qtpandas-1.0.4-py3.8.egg\qtpandas\models目录下的DataFrameModel.py文件中的.ix全部替换为loc即可。

posted on 2021-09-18 16:38  GoGrid  阅读(2760)  评论(0编辑  收藏  举报

导航