对Series进行排序
#生成序列obj
obj=pd.Series([4,9,6,20,4],index=['d','a','e','b','c'])
d 4
a 9
e 6
b 20
c 4
dtype: int64
#按obj的索引排序,默认升序,降序可在括号加ascending=False
obj.sort_index()
a 9
b 20
c 4
d 4
e 6
dtype: int64
#按obj的值排序,默认升序
obj.order()
d 4
c 4
e 6
a 9
b 20
dtype: int64
对DataFrame进行排序
frame=pd.DataFrame(pd.Series([3,5,2,6,9,23,12,34,12,15,11,0]).reshape(3,4),columns=['c','f','d','a'],index=['C','A','B'])
c f d a
C 3 5 2 6
A 9 23 12 34
B 12 15 11 0
frame.sort_index()
c f d a
A 9 23 12 34
B 12 15 11 0
C 3 5 2 6
frame.sort_index(axis=1)
a c d f
C 6 3 2 5
A 34 9 12 23
B 0 12 11 15
frame.sort_index(by='a')
c f d a
B 12 15 11 0
C 3 5 2 6
A 9 23 12 34
frame.sort_index(by=['a','c'])
c f d a
B 12 15 11 0
C 3 5 2 6
A 9 23 12 34