1. .sort_index () method in accordance with the specified axis 索引进行排序
, default ascending
Parameters: .sort_index(axis=0, ascending=True)
default axis0 (0 1 vertical horizontal) Ascending ascending
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(20).reshape(4, 5), index=['c', 'a', 'd', 'b'])
print(df)
0 1 2 3 4
c 0 1 2 3 4
a 5 6 7 8 9
d 10 11 12 13 14
b 15 16 17 18 19
纵向向递增排序df.sort_index()
print(df.sort_index())
0 1 2 3 4
a 5 6 7 8 9
b 15 16 17 18 19
c 0 1 2 3 4
d 10 11 12 13 14
纵向向降序排序df.sort_index(ascending=False)
print(df.sort_index(ascending=False))
0 1 2 3 4
d 10 11 12 13 14
c 0 1 2 3 4
b 15 16 17 18 19
a 5 6 7 8 9
对横轴进行降序排序df.sort_index(axis=1, ascending=False)
print(df.sort_index(axis=1, ascending=False))
4 3 2 1 0
c 4 3 2 1 0
a 9 8 7 6 5
d 14 13 12 11 10
b 19 18 17 16 15
2. .sort_values () method sorted values specified axis, the default ascending
Parameters: Series.sort_values(axis=0, ascending=True)
default axis0 (0 1 vertical horizontal) Ascending Ascending
** parameters: ** DataFrame.sort_values (by, axis = 0, ascending = True) by: an index or index lists shaft axis
在纵轴第二列进行倒叙排序.sort_values(2, ascending=False)
print(df.sort_values(2, ascending=False))
在横轴第二列进行倒叙排序.sort_values(‘a’, axis=1,ascending=False)
print(df.sort_values('a', axis=1, ascending=False))
Note: null NaN into the end of the sort