pandas報錯AttributeError: DataFrame object has no attribute ix問題
pandas報AttributeError: DataFrame object has no attribute ix
在實際操作中有時候需要把采集的數(shù)據(jù)或者分析完的數(shù)據(jù)保存為excel中,列名按照執(zhí)行排序
一開始我采用DataFrame的ix方式去實現(xiàn)是可以達到預(yù)期的,不過最近發(fā)現(xiàn)好像該方法函數(shù)被移除了
運行會拋出以下錯誤:
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,官方移除了一些不推薦使用的方法函數(shù),詳情請參考:
解決方式
根據(jù)官方說明,ix已被移除,可用.iloc替代:
# DataFrame.ix[:, cols] # 已移除,不推薦使用 DataFrame.iloc[:, cols] # 列按指定下標(biāo)排序 cols=【0,2,1】 DataFrame.loc[:, col_header] # 列按指定下標(biāo)排序 cols=【'col','col1'】
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
代碼總結(jié)Python2 和 Python3 字符串的區(qū)別
在本篇文章里小編給大家整理的是一篇關(guān)于Python2 和 Python3 字符串的區(qū)別以及實例代碼,需要的朋友們學(xué)習(xí)下。2020-01-01
用Python和MD5實現(xiàn)網(wǎng)站掛馬檢測程序
系統(tǒng)管理員通常從svn/git中檢索代碼,部署站點后通常首先會生成該站點所有文件的MD5值,如果上線后網(wǎng)站頁面內(nèi)容被篡改(如掛馬)等,可以比對之前生成MD5值快速查找去那些文件被更改,為了使系統(tǒng)管理員第一時間發(fā)現(xiàn),可結(jié)合crontab或nagios等工具2014-03-03

