按升序对索引进行排序 – Python Pandas
pythonserver side programmingprogramming更新于 2025/11/9 5:32:17
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
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

