Python| Pandas Series.argsort()
在Pandas Series.argsort()的帮助下,可以对 pandas 中的系列元素进行排序。但是 pandas 系列的主要内容是我们将输出作为系列中排序元素的索引值。在后面的代码演示中,我们将解释如何将输出作为排序索引值。
Syntax: pandas.Series.argsort(axis=0, kind=’quicksort’, order=None)
Parameters:
axis : It is useful for numpy.
kind : {‘mergesort’, ‘quicksort’, ‘heapsort’}, default ‘quicksort’
order : It is useful for numpy.
Returns: argsorted Series, with -1 indicated where nan values are present
要获取 csv 文件的链接,请单击 nba.csv
代码#1:
在这段代码中,您将看到我们采用了一系列简单的整数值,并尝试根据不同的排序算法方法进行排序,如快速排序、合并排序和堆排序,但默认情况下它会假定为快速排序。让我们看看下面的代码和下面的输出。
# importing pandas
import pandas as pd
# reading the csv
data = pd.read_csv("nba.csv")
data.dropna(inplace = True)
# creating series form weight column
g = pd.Series(data['Weight'].head())
print(g)
gfg = g.argsort(axis = 0, kind ='quicksort', order = None)
print(gfg)
0 180.0
1 235.0
3 185.0
6 235.0
7 238.0
Name: Weight, dtype: float64
0 0
1 2
3 1
6 3
7 4
Name: Weight, dtype: int64
正如您在输出中看到的那样,我们得到这些数字的原因不是按顺序获取排序值,而是看起来很奇怪。这是Series.argsort()
方法的主要概念,它首先返回最小数字的索引值,最后返回最大值的索引值。因为我们有 1 是最小的数字,它的索引值为 4,所以 4 将首先出现,这个概念作为以下输出的流。
代码#2:
# importing pandas
import pandas as pd
# reading the csv
data = pd.read_csv("nba.csv")
data.dropna(inplace = True)
# creating series form weight column
g = pd.Series(data['Weight'].head())
print(g)
gfg = g.argsort(axis = 0, kind ='mergesort', order = None)
print(gfg)
0 180.0
1 235.0
3 185.0
6 235.0
7 238.0
Name: Weight, dtype: float64
0 0
1 2
3 1
6 3
7 4
Name: Weight, dtype: int64
代码#3:
# importing pandas
import pandas as pd
# reading the csv
data = pd.read_csv("nba.csv")
data.dropna(inplace = True)
# creating series form weight column
g = pd.Series(data['Weight'].head())
print(g)
gfg = g.argsort(axis = 0, kind ='heapsort', order = None)
print(gfg)
0 180.0
1 235.0
3 185.0
6 235.0
7 238.0
Name: Weight, dtype: float64
0 0
1 2
3 1
6 3
7 4
Name: Weight, dtype: int64
当我们有缺失值时,输出是什么?
正如我们在上面解释的那样,如果我们想处理缺失值,那么在None的位置它将输出为 -1。
import pandas as pd
# importing pandas
import pandas as pd
# reading the csv
data = pd.read_csv("nba.csv")
# creating series form weight column
g = pd.Series(data['Weight'])
print(g)
gfg = g.argsort(axis = 0, kind ='mergesort', order = None)
print(gfg)
450 226.0
451 206.0
452 234.0
453 203.0
454 179.0
455 256.0
456 231.0
457 NaN
Name: Weight, Length: 458, dtype: float64
450 237
451 41
452 188
453 395
454 330
455 302
456 405
457 -1
Name: Weight, Length: 458, dtype: int64