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

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, None]}, index=[100, 29, 234, 1])
  6. df.sort_index()
  7. --------------------------------------------------
  8. site age price color
  9. 1 pandas 45 4.0 None
  10. 29 baidu 39 2.0 black
  11. 100 google 18 1.0 red
  12. 234 wiki 22 3.0 None

Example

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, None]}, index=[100, 29, 234, 1])
  6. df.sort_index(ascending=False)
  7. --------------------------------------------------------
  8. site age price color
  9. 234 wiki 22 3.0 None
  10. 100 google 18 1.0 red
  11. 29 baidu 39 2.0 black
  12. 1 pandas 45 4.0 None