Sort Pandas Series

Here is the syntax to sort Pandas Series:

(1) Sort Pandas Series in an ascending order:

ser.sort_values(ascending=True, inplace=True)

(2) Sort Pandas Series in a descending order. In this case, simply set ascending=False:

ser.sort_values(ascending=False, inplace=True)

In this guide, you’ll see how to sort Pandas Series that contains:

  • String/text values
  • Numeric values
  • NaN values

Sort Pandas Series that Contains String/Text Values

To start with a simple example, create Pandas Series that contains string/text values:

import pandas as pd

ser = pd.Series(['Emma', 'Maria', 'Bill', 'William', 'Jill', 'Jack'])

print(ser)

Run the code in Python, and you’ll get the following unsorted Series:

0       Emma
1      Maria
2       Bill
3    William
4       Jill
5       Jack

Let’s say that you’d like to sort the Series in an ascending order.

In that case, you’ll need to add the following syntax to the code:

ser.sort_values(ascending=True, inplace=True)

So the complete code to sort the Series in an ascending order is as follows:

import pandas as pd

ser = pd.Series(['Emma', 'Maria', 'Bill', 'William', 'Jill', 'Jack'])

ser.sort_values(ascending=True, inplace=True)

print(ser)

As you can see, the Series is now sorted in an ascending order, where ‘Bill‘ is the first name while ‘William‘ is the last name:

2       Bill
0       Emma
5       Jack
4       Jill
1      Maria
3    William

What if you’d like to sort the Series in a descending order?

In that case, simply set ascending=False as captured below:

import pandas as pd

ser = pd.Series(['Emma', 'Maria', 'Bill', 'William', 'Jill', 'Jack'])

ser.sort_values(ascending=False, inplace=True)

print(ser)

The Series is now sorted in a descending order, where ‘William‘ is the first name, and ‘Bill‘ is the last name:

3    William
1      Maria
4       Jill
5       Jack
0       Emma
2       Bill

Sort Pandas Series that Contains Numeric Values

Let’s now see how to sort a Series that includes numeric values.

To start, create the following Pandas Series that contains only numeric values:

import pandas as pd

ser = pd.Series([45, 99, 23, 15, 117, 72])

print(ser)

Run the code, and you’ll get an unsorted Series with the following numeric data:

0     45
1     99
2     23
3     15
4    117
5     72

You can then sort the Series in an ascending order:

import pandas as pd

ser = pd.Series([45, 99, 23, 15, 117, 72])

ser.sort_values(ascending=True, inplace=True)

print(ser)

You’ll notice that the Series is now sorted in an ascending order, where 15 is the first number while 117 is the last number:

3     15
2     23
0     45
5     72
1     99
4    117

Alternatively, you can sort the Series in a descending order:

import pandas as pd

ser = pd.Series([45, 99, 23, 15, 117, 72])

ser.sort_values(ascending=False, inplace=True)

print(ser)

Here is the result, where 117 appears first, and 15 appears last:

4    117
1     99
5     72
0     45
2     23
3     15

Sort Pandas Series that Contains NaN Values

For the final scenario, create a Series with NaN values using the Numpy library:

import pandas as pd
import numpy as np

ser = pd.Series([45, np.nan, np.nan, 99, 23, 15, 117, np.nan, 72])

print(ser)

As you may observe, the unsorted Series now includes 3 NaNs as highlighted in yellow:

0     45.0
1      NaN
2      NaN
3     99.0
4     23.0
5     15.0
6    117.0
7      NaN
8     72.0

You can then place the NaN values at the top (while the other values will be sorted in an ascending order).

To accomplish this goal, you’ll need to set na_position=’first’ as follows:

import pandas as pd
import numpy as np

ser = pd.Series([45, np.nan, np.nan, 99, 23, 15, 117, np.nan, 72])

ser.sort_values(ascending=True, na_position='first', inplace=True)

print(ser)

The NaN values will now appear at the top, while the remaining values will be sorted in an ascending order:

1      NaN
2      NaN
7      NaN
5     15.0
4     23.0
0     45.0
8     72.0
3     99.0
6    117.0

Alternatively, you can place the NaN values at the bottom (while the other values will be sorted in an ascending order):

import pandas as pd
import numpy as np

ser = pd.Series([45, np.nan, np.nan, 99, 23, 15, 117, np.nan, 72])

ser.sort_values(ascending=True, inplace=True)

print(ser)

You’ll now notice that the NaN values appear at the bottom, while the other values are sorted in an ascending order:

5     15.0
4     23.0
0     45.0
8     72.0
3     99.0
6    117.0
1      NaN
2      NaN
7      NaN

You can read more about sorting a Series by visiting the Pandas Documentation.

Leave a Comment