# How to sort a Pandas Series?

In this problem we have to sort a Pandas series. We will define an unsorted pandas series and will sort it using the sort_values() function in the Pandas library.

## Algorithm

Step 1: Define Pandas series.
Step 2: Sort the series using sort_values() function.
Step 3: Print the sorted series.

## Example Code

import pandas as pd

panda_series = pd.Series([18,15,66,92,55,989])
print("Unsorted Pandas Series: \n", panda_series)

panda_series_sorted = panda_series.sort_values(ascending = True)
print("\nSorted Pandas Series: \n", panda_series_sorted)

## Output

Unsorted Pandas Series:
0     18
1     15
2     66
3     92
4     55
5    989
dtype: int64

Sorted Pandas Series:
1     15
0     18
4     55
2     66
3     92
5    989
dtype: int64

## Explanation

The ascending parameter in the sort_values() function takes in boolean value. If the value is True, it sorts the series in ascending order. If the value is False, it sorts the value in descending order.

Updated on: 16-Mar-2021

220 Views