按升序对索引排序——Python Pandas
sort_index()用于按升序和降序对索引进行排序。如果你不指定任何参数,则索引按照升序排列。
首先,导入所需的库 -
import pandas as pd
创建新 DataFrame。其索引没有排序 -
dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1'])
对索引进行排序 -
dataFrame.sort_index()
示例
代码如下 -
import pandas as pd dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1']) print"DataFrame...\n",dataFrame print"\nSort index...\n",dataFrame.sort_index()
输出
将生成以下输出 -
DataFrame... Col1 4 100 8 150 2 200 9 250 15 250 11 500 Sort index... Col1 2 200 4 100 8 150 9 250 11 500 15 250
广告