Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Pandas Series argsort() Method

In this tutorial, we will learn the python pandas Series.argsort() method. This method returns the Series consisting of the indices that would sort the Series values. If the Series contains null or missing values, Series.argsort() method gives -1 value as its index.

The below is the syntax of the Series.argsort() method.

Syntax

Series.argsort(axis=0, kind='quicksort', order=None)

Parameters

axis: It is 0 or “index”. It has no effect but is accepted for compatibility with numpy.

kind: It includes ‘mergesort’, ‘quicksort’, ‘heapsort’, and the default is ‘quicksort’. It indicates the choice of sorting algorithm.

order: None. It has no effect but is accepted for compatibility with numpy.

Example: Sort the Series elements using the Series.argsort() Method

Let's create a Series and apply the Series.argsort() method to the Series. By default, Series.argsort() method uses the quicksort algorithm to sort the elements of the Series and returns the indices. See the below example.

Even if we use the 'mergesort' and 'heapsort', we will get the same output.

#importing pandas as pd
import pandas as pd
#creating Series
s_1= pd.Series([2,1,4,3])
print(s_1.argsort())


0 1
1 0
2 3
3 2
dtype: int64

Example: Sort the Series Elements consisting of null Values

Let's apply the Series.argsort() method to the Series consisting of null values. The Series.argsort() method returns -1 value as an index for the null values. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
s_1= pd.Series([None,1,None,3])
print(s_1.argsort())


0 -1
1 0
2 -1
3 1
dtype: int64

Conclusion

In this tutorial, we understand the Series.argsort() method of the data frame. We learned the syntax and parameters of Series.argsort() method and created different examples to better understand this topic.