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

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically