pandas报错AttributeError: DataFrame object has no attribute ix问题

来自:网络
时间:2024-03-18
阅读:

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已被移除,可用.iloc替代:

# DataFrame.ix[:, cols] # 已移除,不推荐使用
DataFrame.iloc[:, cols] # 列按指定下标排序 cols=【0,2,1】 
DataFrame.loc[:, col_header] # 列按指定下标排序 cols=【'col','col1'】

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

返回顶部
顶部