0
另一個熊貓排序問題(嘗試所有當前SO的並沒有找到任何解決方案)。排序列標籤熊貓數據透視表
我有一個pandas pivot_table
像這樣:
rows = ['Tool_Location']
cols = ['shift_date','Part_Number', 'shift']
pt = df.pivot_table(index='Tool_Location', values='Number_of_parts', dropna=False, fill_value ='0', columns=cols, aggfunc='count')
生產:
Shift date 10/19
Part_number 40001
shift first second third
tool_loc
T01 0 1 0
T02 2 1 0
我想切換移標籤的順序,以便它是third first second
編輯:
G接近解決方案但沒有看到它。
使用:
col_list = pt.columns.tolist()
output:
[('10/20/16', 'first'), ('10/20/16', 'second'), ('10/20/16', 'third'), ('10/21/16', 'first'), ('10/21/16', 'second'), ('10/21/16', 'third')]
任何人都知道如何動態重新排序中的項目,它的:
[('10/20/16', 'third'), ('10/20/16', 'first'), ('10/20/16', 'second'), ('10/21/16', 'first'), ('10/21/16', 'second'), ('10/21/16', 'second')]
因爲那樣的話,我們可以通過使用PT = PT [col_list]
給我'KeyError異常在/ tooltrack /彙總/ 「[‘第三’‘第一’‘第二’]不是指數」' – TangoAlee
只是改變「秒'到'秒'。從查看上面的數據框 –
對不起 - 它實際上是數據框中的「第二」,我錯過了這個例子。 – TangoAlee