我正在嘗試使用以下代碼對 Python 中的多個索引進行排序,這給了我代碼下面提供的錯誤:raw_data.sort_index(level='employee_id',ascending=False,inplace=True).sort_index(level='PAYCODE_ID',ascending=True,inplace=True)---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-732-d2ad9fdef0b4> in <module>----> 1 raw_data.sort_index(level='employee_id',ascending=False,inplace=True).sort_index(level='PAYCODE_ID',ascending=True,inplace=True)AttributeError: 'NoneType' object has no attribute 'sort_index'我認為在第一個“sort_index”方法之后,python 結果變成“None”對象類型。根據 python 文檔,如果“inplace”參數設置為 True,則返回對象將是一個數據框。另請注意,當我將方法拆分為以下兩行代碼時,它將排序結果存儲到 raw_data 數據幀中:raw_data.sort_index(level='employee_id',ascending=False,inplace=True)
raw_data.sort_index(level='PAYCODE_ID',ascending=True,inplace=True)
1 回答

SMILET
TA貢獻1796條經驗 獲得超4個贊
如果傳遞inplace=True
to sort_index
,該函數將就地運行并返回None
。因此,inplace=True
如果您想鏈接命令,請刪除并分配回來,或者像您發布的那樣執行兩個單獨的命令。
此外,您還可以將多個級別傳遞給sort_index
raw_data.sort_index(level=['employee_id', 'PAYCODE_ID'], ascending=[False, True],inplace=True)
添加回答
舉報
0/150
提交
取消