When working with series from the pandas module in Python, you can easily sort series using the sort_values() function.
s = pd.Series([11, 5, 30, 25, 14])
print(s.sort_values())
#Output:
1 5
0 11
4 14
3 25
2 30
dtype: int64
When working with data, it is very useful to be able to sort data in a list of items to order our data.
Many times when we are working with data in Python, we are using the pandas module and Series objects.
We can easily sort data in a Series object using pandas in Python.
To sort a pandas series, you can use the sort_values() function. The sort_values() function will sort a series by its values in ascending order.
Below is a simple example of how to use sort_values() on a pandas series to sort it by its values.
s = pd.Series([11, 5, 30, 25, 14])
print(s.sort_values())
#Output:
1 5
0 11
4 14
3 25
2 30
dtype: int64
If you want to modify the pandas series itself, you can pass “inplace=True”.
s = pd.Series([11, 5, 30, 25, 14])
s.sort_values(inplace=True)
print(s)
#Output:
1 5
0 11
4 14
3 25
2 30
dtype: int64
Sorting a pandas Series by Values Descending in Python
By default, the sort_values() function when applied to a pandas series sorts the series values in ascending order.
To sort a pandas series in descending order, pass “ascending=False” to sort_values().
Below is an example in Python of how to sort a pandas series descending.
s = pd.Series([11, 5, 30, 25, 14])
print(s.sort_values(ascending=False))
#Output:
2 30
3 25
4 14
0 11
1 5
dtype: int64
How to Sort Series in pandas using key Argument
Sometimes it makes sense to sort a series after the application of a function. We can use the sort_values() ‘key’ parameter to pass a function and sort by the function values.
For example, if we want to sort by the square of each number, we could pass a lambda expression that squares each number in a series.
Below is an example in Python of how to sort a pandas series with the ‘key’ parameter.
s = pd.Series([1, -5, 3, -4, 2])
print(s.sort_values(key=lambda x: x**2))
#Output:
0 1
4 2
2 3
3 -4
1 -5
dtype: int64
Treatment of NaN Values with pandas sort_values() Function
If your series has NaN values, you can specify the treatment of them after sorting using the ‘na_position’ parameter.
You can have NaN values go first or last in your series after sorting by passing ‘first’ and ‘last’ respectively to ‘na_position’.
By default, NaN values go last after sorting with sort_values().
Below is an example using the pandas module of sorting a series with NaN values.
s = pd.Series([1, np.nan, 3, -4, 2])
print(s.sort_values())
#Output:
3 -4.0
0 1.0
4 2.0
2 3.0
1 NaN
dtype: float64
You can have NaN values go first by passing ‘na_position=False’ as shown in the following Python code.
s = pd.Series([1, np.nan, 3, -4, 2])
print(s.sort_values(na_position=False))
#Output:
1 NaN
3 -4.0
0 1.0
4 2.0
2 3.0
dtype: float64
Sorting a pandas Series by Index in Python with sort_index()
If you want to sort the index of a pandas series, you can use the sort_index() function.
sort_index() sorts the index, and has all of the same parameters and keywords (ascending, inplace, key, etc.) as the sort_values() function.
Below is a simple example in Python of how to sort a pandas series by its index.
s = pd.Series(['a', 'b', 'c', 'd'], index=[2, 4, 1, 3])
print(s.sort_index())
#Output:
1 c
2 a
3 d
4 b
dtype: object
Hopefully this article has been useful for you to learn how to sort a series when using the pandas module in Python.