How to Sort Series

Here’s the tutorial of how to sort series in Pandas.

how to sort series in reverse order

Pandas is a powerful Python library for data analysis. One of the things that Pandas makes easy is sorting data. In this chapter, we will learn how to sort series in Pandas.

This is the code which contains my series I’d like to sort.

import pandas as pd
import numpy as np

my_series = pd.Series((3,5,2,6,np.nan,1,7,4))

print(f"This is my series: \n{my_series}")

How to sort series in ascending order

The most common way to sort a series in Pandas is to use the sort_values() method. The sort_values() method takes two arguments:

The first argument is the column or index that you want to sort by.
The second argument is the ascending parameter, which specifies whether you want to sort the values in ascending or descending order. The default value for the ascending parameter is True, so when sorting in ascending order you don’t need to specify it.

For example, the following code sorts the series my_series in ascending order:

import pandas as pd
import numpy as np

my_series = pd.Series((3,5,2,6,np.nan,1,7,4))

print(f"This is my series sorted in ascending order: \n{my_series.sort_values()}")

As you can see, the series my_series is now sorted in ascending order.

How to sort series in descending order

To sort a series in descending order, you can simply set the ascending parameter to False. For example, the following code sorts the series my_series in descending order:

import pandas as pd
import numpy as np

my_series = pd.Series((3,5,2,6,np.nan,1,7,4))

print(f"This is my series sorted in descending order: \n{my_series.sort_values(ascending=False)}")

The default value for the ascending parameter is True, so when sorting in descending order you need to use it and change the default value to False.

How to sort NaN values

By default, NaN values are sorted to the end of the series. However, you can also sort NaN values to the beginning of the series by setting the na_position parameter to ‘first’. For example, the following code sorts the series my_series with NaN values to the beginning:

import pandas as pd
import numpy as np

my_series = pd.Series((3,5,2,6,np.nan,1,7,4))

print(f"This is my series having NaN values sorted: \n{my_series.sort_values(na_position='first')}")

The default value of the na_position parameter is ‘last’.

Sort_Values function documentation can be found here.

How to sort series in reverse order

In addition to sorting series in ascending or descending order, you can also sort them in reverse order. To do this, you can use the sort_index() method with the ascending parameter set to False. For example, the following code sorts the series my_series in reverse order:

import pandas as pd
import numpy as np

my_series = pd.Series((3,5,2,6,np.nan,1,7,4))

print(f"This is my series sorted in reverse order: \n{my_series.sort_index(ascending=False)}")

how to sort series in reverse order

As you can see, the series my_series is now sorted in reverse order.

How to Sort a Series by Multiple Columns

You can also sort a series by multiple columns. To do this, you can use the sort_values() method and pass a list of columns to the by parameter. The columns will be sorted in the order that they are specified in the list. For example, the following code sorts the series my_series by the index column and then by the values column:

import pandas as pd
import numpy as np

my_series = pd.Series((3,5,2,6,np.nan,1,7,4))

print(f"This is my series: \n{my_series}")

print(f"This is my series sorted by index and value: \n{my_series.sort_values(by=['index', 'values'])}")

The series my_series is now sorted by the index column and then by the values column.

Sort_Index function documentation can be found here.

This Post Has One Comment

Leave a Reply