In actual operation, sometimes it is necessary to save the collected data or the analyzed data in excel, and the column names are sorted according to the execution. At first, I used the ix method of DataFrame to achieve the expectation, but recently I found that this method function Is removed, the operation will throw the following error:
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'
root cause
Due to the installation of a higher version of pandas, some deprecated methods and functions have been officially removed. For details, please refer to:
Click to view the official removal of deprecated functions
Solution
According to official instructions, ix has been removed and can be replaced by .iloc:
# DataFrame.ix[:, cols] # 已移除,不推荐使用
DataFrame.iloc[:, cols] # 列按指定下标排序 cols=【0,2,1】
DataFrame.loc[:, col_header] # 列按指定下标排序 cols=【'col','col1'】