Python Pandas——返回已排序的索引副本


要在Pandas中返回已排序的索引副本,请使用**index.sort_values()**方法。首先,导入所需的库 -

import pandas as pd

创建Pandas索引 -

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

显示Pandas索引 -

print("Pandas Index...\n",index)

对索引值进行排序。默认情况下,按升序排序 -

print("\nSort the index values...\n",index.sort_values())

例子

以下为代码 -

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
# By default, sorts in Ascending order
print("\nSort the index values...\n",index.sort_values())

输出

这将产生以下输出 -

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...
Int64Index([10, 30, 50, 70, 90, 95, 110], dtype='int64')

更新于: 14-Oct-2021

138 浏览

开启您的 职业生涯

完成课程即可获得认证

开始
广告