sort_index排序
ser1=Series(np.arange(4),index=[‘b‘,‘c‘,‘d‘,‘a‘]) print(ser1) print(‘-‘*20) print(ser1.sort_index())#通过列或者行进行排序 print(‘-‘*20) print(ser1.sort_index(ascending=False))#False表示反过来排
b 0
c 1
d 2
a 3
dtype: int32
--------------------
a 3
b 0
c 1
d 2
dtype: int32
--------------------
d 2
c 1
b 0
a 3
dtype: int32
df=DataFrame(np.random.randint(1,9,(4,4)), columns=[‘b‘,‘c‘,‘a‘,‘d‘], index=[‘B‘,‘D‘,‘A‘,‘C‘]) print(df) print(‘-‘*20) print(df.sort_index())#默认是对行索引进行排序 print(‘-‘*20) print(df.sort_index(axis=1,ascending=False))#对列索引进行反向排序
b c a d
B 4 7 8 2
D 1 2 4 3
A 1 6 1 5
C 8 6 2 5
--------------------
b c a d
A 1 6 1 5
B 4 7 8 2
C 8 6 2 5
D 1 2 4 3
--------------------
d c b a
B 2 7 4 8
D 3 2 1 4
A 5 6 1 1
C 5 6 8 2