Python | Панды Series.argsort ()

Опубликовано: 27 Марта, 2022

С помощью Pandas Series.argsort () можно сортировать элементы серий в пандах. Но главное в pandas series - это то, что мы получаем результат в виде значений индекса отсортированных элементов в серии. В более поздней демонстрации кода мы объясним, как мы получаем вывод в виде отсортированных значений индекса .

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

Code #1 :
In this code you will see that we are taking a simple series of some integer values and try to sort on the bases of different methods of sorting algorithms like quicksort, mergesort and heapsort but by default it will assume as quicksort. Lets see the code below and the following output.

# 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)
Output:
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

As you can see in the output and it looks strange that instead of getting the sorted values in series why we got these numbers. This is the major concept of Series.argsort() method it return the index value of a smallest number first and index value of largest value in the end. As we have 1 is the smallest number and it’s index value is 4 then 4 will come first and this concept as flow as following output.

Code #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)
Output:
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

Code #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)
Output:
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

Что будет на выходе, если у нас есть пропущенные значения?

As we have explained above that if we want to handle the missing values then in the place of None it will give the output as -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)
Output:
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

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.