DataFrame.sort_index
DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’, sort_remaining=True, ignore_index=False, key=None)
按标签(沿轴)对对象进行排序。
Parameters
| axis | 0 or index:作用于行;1 or columns:作用于列 |
|---|---|
| level | 指定索引级别 |
| ascending | 升序与降序 |
| inplace | False:返回一个副本;True:就地执行操作并返回None |
| kind | 排序算法的选择:quicksort、mergesort、heapsort、stable |
| na_position | first:把NaN值放在开头;last:把NaN值放在末尾 |
| sort_remaining | |
| ignore_index | True:轴将被标记为0,1,…,n-1 |
| key | 在排序之前将键函数应用于值。类似于内置sorted()函数中的key |
Example
import pandas as pddf = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, None]}, index=[100, 29, 234, 1])df.sort_index()--------------------------------------------------site age price color1 pandas 45 4.0 None29 baidu 39 2.0 black100 google 18 1.0 red234 wiki 22 3.0 None
Example
import pandas as pddf = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, None]}, index=[100, 29, 234, 1])df.sort_index(ascending=False)--------------------------------------------------------site age price color234 wiki 22 3.0 None100 google 18 1.0 red29 baidu 39 2.0 black1 pandas 45 4.0 None
