Python Pandas – 返回按降序排列的索引的排序副本
要返回按降序排列的索引的排序副本,请使用 Pandas 中的 index.sort_values() 方法。参数 ascending 设置为 False 。
首先,导入所需的库 –
import pandas as pd
创建 Pandas 索引 –
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
显示 Pandas 索引 –
print("Pandas Index...\n",index)
排序索引值。若要按降序排序值,则将 “ascending” 参数设置为 “False” –
print("\nSort the index values in descending order...\n",index.sort_values(ascending=False))
示例
以下是代码 –
import pandas as pd
# 创建 Pandas 索引
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
# 显示 Pandas 索引
print("Pandas Index...\n",index)
# 返回索引中的元素数
print("\nNumber of elements in the index...\n",index.size)
# 返回数据类型
print("\nThe dtype object...\n",index.dtype)
# 排序索引值
# 若要按降序排序值,则将 "ascending" 参数设置为 "False"
print("\nSort the index values in descending order...\n",index.sort_values(ascending=False))
输出
这将产生以下输出 –
Pandas Index...
Int64Index([50, 10, 70, 95, 110, 90, 30], dtype='int64')
Number of elements in the index...
7
The dtype object...
int64
Sort the index values in descending order...
Int64Index([110, 95, 90, 70, 50, 30, 10], dtype='int64')
极客教程