鑑於這種數據幀和樞軸表:熊貓透視表嵌套排序
import pandas as pd
df=pd.DataFrame({'A':['x','y','z','x','y','z'],
'B':['one','one','one','two','two','two'],
'C':[7,5,3,4,1,6]})
df
A B C
0 x one 7
1 y one 5
2 z one 3
3 x two 4
4 y two 1
5 z two 6
table = pd.pivot_table(df, index=['A', 'B'],aggfunc=np.sum)
table
A B
x one 7
two 4
y one 5
two 1
z one 3
two 6
Name: C, dtype: int64
我要排序的透視表,使得「A」的順序是Z,X,Y和B」的順序'基於數據幀列'C'中遞減排序的值。
像這樣:
A B
z two 6
one 3
x one 7
two 4
y one 5
two 1
Name: C, dtype: int64
提前感謝!
z,x ,y'沒有按任何順序排序。 – Alexander
嗨亞歷山大。是的,那是因爲我想能夠自定義排序第一個索引的順序。 –