In this tutorial, we will look at how to sort values of a pandas series.
How to sort 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
.
Examples
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.
1. Sort Series in Ascending Order
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.
2. Sort Series in Descending Order
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 –
- Convert Pandas Series to a DataFrame
- Convert Pandas Series to a List
- Convert Pandas Series to a NumPy Array
- Convert Pandas Series to a Dictionary
- Sort a Pandas Series
- Append Two Pandas Series
- Apply a Function to a Pandas Series
- Pandas – Shift column values up or down
- Plot a Histogram of Pandas Series Values
- Create a Pie Chart of Pandas Series Values
- Plot a Bar Chart of Pandas Series Values
- Create a Boxplot from Pandas Series Values
- Create a Density Plot from Pandas Series Values