Pandas Series.argsort() method: Tutorial & Examples

Updated: February 18, 2024 By: Guest Contributor Post a comment

Introduction

Pandas is a powerful library in Python widely used for data manipulation and analysis. Within pandas, the Series object is one-dimensional, capable of holding any data type, akin to a column in a spreadsheet. In this tutorial, we will delve into the argsort() method of the Pandas Series object. The argsort() method returns the integer indices that would sort the Series values, providing a useful tool for data sorting and rearrangement.

Before diving into examples, ensure you have pandas installed and imported:

import pandas as pd

Basic Usage of argsort()

The simplest form of using argsort() can be demonstrated in sorting a series of random numbers:

import numpy as np # For generating random numbers

# Create a pandas Series
s = pd.Series(np.random.rand(5))
print('Original Series:\n', s)

# Use argsort
ind = s.argsort()
print('Indices that would sort the Series:\n', ind)

Output might look like:

Original Series:
 0    0.423560
 1    0.765431
 2    0.978023
 3    0.254785
 4    0.597649

Indices that would sort the Series:
 3    0
 0    1
 4    2
 1    3
 2    4

This shows the indices in the Series that, if followed, would sort the Series. Hence, index 3 has the smallest value, followed by index 0, and so on.

Sorting with Missing Values

Handling missing values is an important part of data analysis. Let’s see how argsort() behaves with missing values:

# Creating a Series with missing values
s = pd.Series([np.nan, 1, 3, 2, np.nan])
print('Original Series with NaN:\n', s)

# Use argsort, noting behavior with NaN
ind = s.argsort()
print('Indices for sorting (NaN are last):\n', ind)

Notice how argsort() places NaN values at the end by default, which helps in retaining dataset integrity during analysis.

Complex Sorting with argsort()

Moving on to more complex uses, let’s apply argsort() to sort one series based on the values of another. This is particularly useful in data frames where you’d like to sort the values of one column based on the sorting order of another column.

# Create two Series
s1 = pd.Series([5, 1, 3, 2, 4])
s2 = pd.Series(['a', 'b', 'c', 'd', 'e'])

# Sort s1 and use the resulting indices to sort s2
sorted_indices = s1.argsort()
s2_sorted = s2[sorted_indices]
print('s2 sorted based on s1:\n', s2_sorted)

This example clearly illustrates the versatility of argsort() in reordering data based on entirely different datasets while maintaining a logical and clean code structure.

Comparing argsort() with sort_values()

In practice, argsort() might often be compared or used interchangeably with sort_values(), another pandas method for sorting. However, a key difference is that sort_values() sorts the series directly and returns the sorted series, whereas argsort() returns the indices that would sort the series. Depending on the use case, both methods are invaluable tools.

# Comparison example
s = pd.Series([10, 21, 34, 20, 45])
s_sorted_via_argsort = ns[ns.argsort()]
ns_sorted_via_sort_values = ns.sort_values()

print('Sorted via argsort:\n', ns_sorted_via_argsort)
print('Sorted directly via sort_values:\n', ns_sorted_via_sort_values)

Conclusion

Throughout this tutorial, we explored the argsort() method from basic to advanced uses, demonstrating its power and flexibility in sorting mechanisms within pandas’ ecosystem. Whether dealing with missing values, sorting based on external data, or understanding its differentiation from sort_values(), argsort() proves to be a critical tool for Python data analysts and scientists.