
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
- Related Articles
- How to append elements to a Pandas series?
- How to append a pandas Series object to another Series in Python?
- How to remove NaN from a Pandas Series?
- How to sort a column of a Pandas DataFrame?
- Python Pandas - How to Sort MultiIndex
- How to prefix string to a pandas series labels?
- How to sort multiple columns of a Pandas DataFrame?
- How to create a Pandas series from a python dictionary?
- How to create a series from a list using Pandas?
- How to unnest (explode) a row in a pandas series?
- How to suffix a string to pandas series index labels?
- Python Pandas - How to Sort MultiIndex at a specific level
- How to get the nth percentile of a Pandas series?
- How to get few rows from a Series in Pandas?
- How to check the data type of a pandas series?

Advertisements