Python | Pandas Series.argsort ()

Series.argsort() Pandas Series.argsort() returns indexes that will sort the underlying data this series object.

Syntax: Series.argsort (axis = 0, kind = `quicksort`, order = None)

Parameter:
axis: Has no effect but is accepted for compatibility with numpy.
kind: {`mergesort`, ` quicksort `,` heapsort `}, default` quicksort `
order: Has no effect but is accepted for compatibility with numpy.

Returns: argsorted: Series, with -1 indicated where nan values ​​are present

Example # 1: Use Series.argsort () to return an index sequence that will sort the underlying data of the given series object.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ 34 , 5 , 13 , 32 , 4 , 15 ])

 
# Create index

index_ = [ `Coca Cola` , `Sprite` , ` Coke` , `Fanta` , `Dew` , `ThumbsUp` ]

  
# set index

sr.index = index_

  
# Print series

print (sr)

Output:

 Coca Cola 34 Sprite 5 Coke 13 Fanta 32 Dew 4 ThumbsUp 15 dtype: int64  

We will now use Series.argsort () to return a sequence of indices that will sort the underlying data for this series object.

 

# return indices that will
# sort the series

result = sr.argsort ()

 
# Print the result

print ( result)

 
# Let`s sort the series by result

print (sr [result])

Output:

 Coca Cola 4 Sprite 1 Coke 2 Fanta 5 Dew 3 ThumbsUp 0 dtype : int64 Dew 4 Sprite 5 Coke 13 ThumbsUp 15 Fanta 32 Coca Cola 34 dtype: int64 

As we can see from the output, Series.argsort () mustache Has successfully returned a series object containing the indexes that will sort the given series object.

Example # 2: Use Series.argsort () to return the index sequence which will sort the underlying data of this series object.

# import pandas as pd

import pandas as pd

  
# Create series

sr = pd.Series ([ 11 , 21 , 8 , 18 , 65 , 18 , 32 , 10 , 5 , 32 , None ])

 
# Create index
# apply annual rate

index_ = pd.date_range ( ` 2010-10-09 08:45` , periods = 11 , freq = `Y` )

 
# install index

sr.index = index_

 
# Print series

print (sr)

Output:

 2010-12-31 08:45:00 11.0 2011-12-31 08:45:00 21.0 2012 -12-31 08:45:00 8.0 2013-12-31 08:45:00 18.0 2014-12-31 08:45:00 65.0 2015-12-31 08:45:00 18.0 2016-12-31 08: 45:00 32.0 2017-12-31 08:45:00 10.0 2018-12-31 08:45:00 5.0 2019-12-31 08:45:00 32.0 2020-12-31 08:45:00 NaN Freq: A-DEC, dtype: float64 

We will now use Series.argsort () to return a sequence of indices that will sort the underlying data of a given series object.

# return index che who will
# sort the series

result = sr.argsort ()

 
# Print result

print (result)

 
# Let`s sort the series by result

print (sr [result])

Output:

 2010-12-31 08:45:00 8 2011-12-31 08:45 : 00 2 2012-12-31 08:45:00 7 2013-12-31 08:45:00 0 2014-12-31 08:45:00 3 2015-12-31 08:45:00 5 2016-12 -31 08:45:00 1 2017-12-31 08:45:00 6 2018-12-31 08:45:00 9 2019-12-31 08:45:00 4 2020-12-31 08:45: 00 -1 Freq: A-DEC, dtype: int64 2018-12-31 08:45:00 5.0 2012-12-31 08:45:00 8.0 2017-12-31 08:45 : 00 10.0 2010-12-31 08:45:00 11.0 2013-12-31 08:45:00 18.0 2015-12-31 08:45:00 18.0 2011-12-31 08:45:00 21.0 2016-12 -31 08:45:00 32.0 2019-12-31 08:45:00 32.0 2014-12-31 08:45:00 65.0 2020-12-31 08:45:00 NaN dtype: float64 

As we can see from the output, Series.argsort () has successfully returned a series object containing the indices that will sort this series object. Note that the function returned -1 as the index position for missing values.