Python | Pandas Series.argsort()
With the help of Pandas Series.argsort(), one can sort the elements of series in pandas. But the main thing in pandas series is we get the output as index values of the sorted elements in series. In the later code demonstration, we will explain that how we get the output as sorted index values.
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
To get the link to csv file, click on 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 basis 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.
Python3
# 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
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 :
Python3
# 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
Code #3 :
Python3
# 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
What is the output when we have missing values ? 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.
Python3
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
Contact Us