Change language

Python | Pandas Series.argsort ()

| | |

Pandas Series.argsort () can be used to sort series items in pandas. But the main thing in the series of pandas — this is what we get the output as index values ​​ of the sorted items in the series. In a later code demo, we’ll explain 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 a link to the CSV file, click on nba.csv

Code # 1:
In this code, you will see that we take a simple series of some integer values ​​and try to sort based on different sorting algorithms like quicksort, and merge heapsort , but by default it will accept the sort. Let’s see the code below and the following output.

# pandas import

import pandas as pd 

 
# read CSV

data = pd. read_csv ( "nba.csv" )

 

data.dropna (inplace = True )

 
# create a series form weight column

g = pd.Series (data [ ’ Weight ’ ]. head ())

print (g)

 

gfg = g.argsort (axis = 0 , kind = ’quicksort’ , order = None )

 

print (gfg)

Exit:

 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 sorted values ​​sequentially oh why we got these numbers. This is the basic concept of the Series.argsort () method it returns the index value of the smallest number first and the index value of the largest value at the end. Since we have 1 — this is the smallest number and its index value is 4, then it will be 4 first, and this concept will look like a stream following the output.

Code # 2:

# pandas import

import pandas as pd 

 
# read CSV

data = pd.read_csv ( " nba.csv " )

  

data.dropna (inplace = True )

  
# create a series form weight column

g = pd.Series (data [ ’Weight’ ]. Head ())

print (g)

 

gfg = g.argsort (axis = 0 , kind = ’ mergesort’ , order = None )

 

print (gfg)

Exit:

 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:

# pandas import

import pandas as pd 

 
# read CSV

data = pd.read_csv ( "nba.csv" )

 

data.dropna (inplace = True )

 
# create series form weight column

g = pd.Series (data [ ’Weight’ ]. head ())

print (g)

 

gfg = g.argsort (axis = 0 , kind = ’heapsort’ , order = None )

 

print (gfg)

Exit:

 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 displayed when we have missing values?

As we explained above, if we want to handle missing values it will give -1 instead of None .

import pandas as pd

 
# pandas import

import pandas as pd 

 
# CSV reading

data = pd.read_csv ( "nba.csv" )

 
# create 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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method