Python | Pandas Series.argsort()
Last Updated :
27 Feb, 2019
Improve
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
Python3
Output :
Python3 1==
Output :
Python3
Output :
Python3 1==
Output :
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 presentExample #1: Use
Series.argsort()
function to return the sequence of index which will sort the underlying data of the given series object.
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([34, 5, 13, 32, 4, 15])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
Coca Cola 34 Sprite 5 Coke 13 Fanta 32 Dew 4 ThumbsUp 15 dtype: int64Now we will use
Series.argsort()
function to return a sequence of indices which will sort the underlying data of the given series object.
# return the indices which will
# sort the series
result = sr.argsort()
# Print the result
print(result)
# Let's sort the series using the result
print(sr[result])
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: int64As 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.
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([11, 21, 8, 18, 65, 18, 32, 10, 5, 32, None])
# Create the Index
# apply yearly frequency
index_ = pd.date_range('2010-10-09 08:45', periods = 11, freq ='Y')
# set the index
sr.index = index_
# Print the series
print(sr)
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: float64Now we will use
Series.argsort()
function to return a sequence of indices which will sort the underlying data of the given series object.
# return the indices which will
# sort the series
result = sr.argsort()
# Print the result
print(result)
# Let's sort the series using the result
print(sr[result])
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: float64As 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.