Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.argsort()
function returns the indices that would sort the underlying data of the given 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()
function to return the sequence of index which will sort the underlying data of the given series object.
import pandas as pd
sr = pd.Series([ 34 , 5 , 13 , 32 , 4 , 15 ])
index_ = [ 'Coca Cola' , 'Sprite' , 'Coke' , 'Fanta' , 'Dew' , 'ThumbsUp' ]
sr.index = index_
print (sr)
|
Output :
Coca Cola 34
Sprite 5
Coke 13
Fanta 32
Dew 4
ThumbsUp 15
dtype: int64
Now we will use Series.argsort()
function to return a sequence of indices which will sort the underlying data of the given series object.
result = sr.argsort()
print (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 in the output, the Series.argsort()
function has successfully returned a series object containing the indices which will sort the given series object.
Example #2 : Use Series.argsort()
function to return the sequence of index which will sort the underlying data of the given series object.
import pandas as pd
sr = pd.Series([ 11 , 21 , 8 , 18 , 65 , 18 , 32 , 10 , 5 , 32 , None ])
index_ = pd.date_range( '2010-10-09 08:45' , periods = 11 , freq = 'Y' )
sr.index = index_
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
Now we will use Series.argsort()
function to return a sequence of indices which will sort the underlying data of the given series object.
result = sr.argsort()
print (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 in the output, the Series.argsort()
function has successfully returned a series object containing the indices which will sort the given series object. Notice the function has returned -1 as the index position for the missing values.