Sort a Pandas Series

In this tutorial, we will look at how to sort values of a pandas series.

To sort a pandas series, you can use the pandas series sort_values() function. It sorts the series in ascending order by default. You can also specify your preference using the ascending parameter which is True by default. The following is the syntax:

# using pandas series sort_values()
s_sorted = s.sort_values(ascending=True)

Here, s is the pandas series you want to sort. The sort_values() function returns a sorted copy of the original series. To modify the original series inplace, pass inplace=True.

Let’s look at some examples of sorting a pandas series. First, we’ll create a sample pandas series which we will be using throughout this tutorial.

import pandas as pd

# pandas series of Scores of students in a Math Test
student_scores = pd.Series(index=['Steve', 'Raj', 'Emma', 'Hasan', 'Chad', 'Kyle'],
                   data=[58, 72, 81, 93, 64, 70],
                   name='Math Score')

# display the series
print(student_scores)

Output:

Steve    58
Raj      72
Emma     81
Hasan    93
Chad     64
Kyle     70
Name: Math Score, dtype: int64

You can see the values of the series object above. We now have a pandas series containing the scores of six students in a Math class.

By default, the pandas series sort_values() function sorts the series in ascending order. Let’s sort the “student_scores” series created above.

# sort the series
student_scores_sorted = student_scores.sort_values()
# display the sorted series
print(student_scores_sorted)

Output:

Steve    58
Chad     64
Kyle     70
Raj      72
Emma     81
Hasan    93
Name: Math Score, dtype: int64

You can see that the returned series is sorted. We can say that between the six students Steve got the lowest score and Hasan the highest.

Note that the sort_values() function returns a sorted copy of the original series. To modify the original series in-place, pass inplace=True to the function.

To sort the above series in descending order, use the sort_values() function with ascending=False. Let’s now sort the “student_scores” series in descending order.

# sort the series in descending order
student_scores_sorted = student_scores.sort_values(ascending=False)
# display the sorted series
print(student_scores_sorted)

Output:

Hasan    93
Emma     81
Raj      72
Kyle     70
Chad     64
Steve    58
Name: Math Score, dtype: int64

You can see that now the returned series is sorted in descending order. The choice of which order to sort the series in depends on the use-case. For example, a teacher might want to know the top scores in a quiz, in such a case sorting in descending order would be a better solution.

For more on the pandas series sort_values() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Tutorials on pandas series –