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

# Creating Pandas index
index = pd.Index([50, 10, 70, 95, 110, 90, 30])

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# Sort index values
# To sort values in Descending order, set the "ascending" parameter to "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')

更新时间: 2021 年 10 月 14 日

291 次浏览

开始你的 职业生涯

完成课程进行认证

开始
广告
© . All rights reserved.