7
Q
定製與熊貓
A
回答
18
一個方法是使用自定義字典來創建一個「等級」欄,然後我們用它來進行排序與再分類後除去列:
In [17]:
custom_dict = {'Critical':0, 'High':1, 'Medium':2, 'Low':3}
df['rank'] = df['Criticality'].map(custom_dict)
df
Out[17]:
Name Criticality rank
0 baz High 1
1 foo Critical 0
2 baz Low 3
3 foo Medium 2
4 bar High 1
5 bar Low 3
6 bar Medium 2
[7 rows x 3 columns]
In [19]:
# now sort by 'Name' and 'rank', it will first sort by 'Name' column first and then 'rank'
df.sort(columns=['Name', 'rank'],inplace=True)
df
Out[19]:
Name Criticality rank
4 bar High 1
6 bar Medium 2
5 bar Low 3
0 baz High 1
2 baz Low 3
1 foo Critical 0
3 foo Medium 2
[7 rows x 3 columns]
In [21]:
# now drop the 'rank' column
df.drop(labels=['rank'],axis=1)
Out[21]:
Name Criticality
4 bar High
6 bar Medium
5 bar Low
0 baz High
2 baz Low
1 foo Critical
3 foo Medium
[7 rows x 2 columns]
+0
非常感謝,這工作就像一個魅力! – Blark
相關問題
- 1. 大熊貓與熊貓
- 2. 與大熊貓
- 3. 與熊貓
- 4. 與熊貓
- 5. 與熊貓
- 6. 與熊貓
- 7. 熊貓:與楠
- 8. 與大熊貓
- 9. 與熊貓
- 10. 與熊貓
- 11. 與大熊貓
- 12. 與大熊貓
- 13. Python熊貓繪製
- 14. 熊貓設置與複製警告
- 15. 繪製與大熊貓和matplotlib
- 16. 與點大熊貓
- 17. 大熊貓與GROUPBY
- 18. 與凡在熊貓
- 19. 問題與熊貓
- 20. 權與大熊貓
- 21. 熊貓:與條件
- 22. 熊貓:與NaN的
- 23. 熊貓箱與== 0
- 24. 熊貓MemoryError與read_sql_query
- 25. 與熊貓/ Python的
- 26. 熊貓:與條件
- 27. 大熊貓與ISIN
- 28. 大熊貓慣用方式定製fillna
- 29. 蟒蛇大熊貓定製AGG功能
- 30. 如何制定排在熊貓
根據您發佈的最終結果,我認爲您實際上首先需要按名稱排序,然後是Criticality。對? – exp1orer
@ exp1orer是的,但沒有使用外部命令,你最終在關鍵性的alpha排序上不是所需的輸出 – EdChum
EdChum,完全正確。我剛纔問了一下,因爲他的問題說他想要「先按重要性再按名稱排序」。但樣本輸出表明否則。 – exp1orer