📜  Python|熊猫索引.argsort()

📅  最后修改于: 2022-05-13 01:55:43.267000             🧑  作者: Mango

Python|熊猫索引.argsort()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。

Pandas Index.argsort()函数返回对索引进行排序的整数索引。默认情况下,排序顺序已设置为递增顺序。

示例 #1:使用Index.argsort()函数查找对给定索引进行排序的索引顺序。

# importing pandas as pd
import pandas as pd
  
# Creating the Index
df = pd.Index([17, 69, 33, 5, 10, 74, 10, 5])
  
# Print the Index
df

输出 :

让我们找到对索引进行排序的索引的顺序。

# to find the ordering of indices 
# that would sort the df Index
df.argsort()

输出 :

正如我们在输出中看到的,该函数返回了索引的排序,这将对给定的索引进行排序。我们可以通过根据排序打印索引来验证这一点。

# Printing the Index based on the
# result of the argsort() function
df[df.argsort()]

输出 :

正如我们在输出中看到的,它是按排序顺序打印的。示例 #2:使用Index.argsort()函数查找对给定索引进行排序的索引顺序。

# importing pandas as pd
import pandas as pd
  
# Creating the Index
df = pd.Index(['Sam', 'Alex', 'Olivia',
          'Dan', 'Brook', 'Katherine'])
  
# Print the Index
df

输出 :

让我们找到对索引进行排序的索引的顺序。

# to find the ordering of indices
# that would sort the df Index
df.argsort()

输出 :

正如我们在输出中看到的,该函数返回了索引的排序,这将对给定的索引进行排序。我们可以通过根据排序打印索引来验证这一点。

# Printing the Index based on the 
# result of the argsort() function
df[df.argsort()]

输出 :

正如我们在输出中看到的,它是按排序顺序打印的。