How to Sort Pandas Series (examples included)

Here is the syntax to sort Pandas Series:

(1) Sort Pandas Series in an ascending order:

import pandas as pd

mySeries = pd.Series(['Item1','Item2','Item3',...])
sortedSeries = mySeries.sort_values(ascending=True)

print(sortedSeries)

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

import pandas as pd

mySeries = pd.Series(['Item1','Item2','Item3',...])
sortedSeries = mySeries.sort_values(ascending=False)

print(sortedSeries)

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, let’s create Pandas Series that contains string/text values:

import pandas as pd

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

print(mySeries)

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

How to Sort Pandas Series

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:

sortedSeries = mySeries.sort_values(ascending=True)

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

import pandas as pd

mySeries = pd.Series(['Emma','Maria','Bill','William','Jill','Jack'])
sortedSeries = mySeries.sort_values(ascending=True)

print(sortedSeries)

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:

Ascending order

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

mySeries = pd.Series(['Emma','Maria','Bill','William','Jill','Jack'])
sortedSeries = mySeries.sort_values(ascending=False)

print(sortedSeries)

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

Descending order

Sort Pandas Series that Contains Numeric Values

So far, you have seen how to sort a Series that contains string/text 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

mySeries = pd.Series([45,99,23,15,115,72])

print(mySeries)

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

Sort numeric data

You can then sort the Series in an ascending order:

import pandas as pd

mySeries = pd.Series([45,99,23,15,117,72])
sortedSeries = mySeries.sort_values(ascending=True)

print(sortedSeries)

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:

Sort Pandas Series

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

import pandas as pd

mySeries = pd.Series([45,99,23,15,117,72])
sortedSeries = mySeries.sort_values(ascending=False)

print(sortedSeries)

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

How to Sort Pandas Series

Sort Pandas Series that Contains NaN Values

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

import pandas as pd
import numpy as np

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

print(mySeries)

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

NaN values

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

mySeries = pd.Series([45,np.nan,np.nan,99,23,15,117,np.nan,72])
sortedSeries = mySeries.sort_values(ascending=True,na_position='first')

print(sortedSeries)

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

Sort Pandas Series with NaN values

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

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

sortedSeries = mySeries.sort_values(ascending=True)

print(sortedSeries)

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

Sort Pandas Series with NaN values

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