
- 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
Python Pandas CategoricalIndex - Reorder categories
To eorder categories, use the CategoricalIndex reorder_categories() method in Pandas. At first, import the required libraries −
import pandas as pd
CategoricalIndex can only take on a limited, and usually fixed, number of possible values. Set the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter −
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
Display the CategoricalIndex −
print("CategoricalIndex...\n",catIndex)
Get the categories −
print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)
Reorder categories using reorder_categories(). Set the categories in new order as a parameter −
print("\nCategoricalIndex after reordering categories...\n",catIndex.reorder_categories(["r", "s", "q", "p"]))
Example
Following is the code −
import pandas as pd # CategoricalIndex can only take on a limited,and usually fixed, number of possible values (categories # Set the categories for the categorical using the "categories" parameter # Treat the categorical as ordered using the "ordered" parameter catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) # Display the CategoricalIndex print("CategoricalIndex...\n",catIndex) # Get the categories print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories) # Reorder categories using reorder_categories() # Set the categories in new order as a parameter print("\nCategoricalIndex after reordering categories...\n",catIndex.reorder_categories(["r", "s", "q", "p"]))
Output
This will produce the following output −
CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') DisplayingCategories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') CategoricalIndex after reordering categories... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['r', 's', 'q', 'p'], ordered=True, dtype='category')
- Related Articles
- Python Pandas CategoricalIndex - Rename categories
- Python Pandas CategoricalIndex - Add new categories
- Python Pandas CategoricalIndex - Rename categories with lambda
- Python Pandas CategoricalIndex - Rename categories with dict-like new categories
- Python Pandas - Remove the specified categories from CategoricalIndex
- Python Pandas CategoricalIndex - Get the categories of this categorical
- Python Pandas CategoricalIndex - Check whether the categories have an ordered relationship
- Python Pandas - Set the categories of the CategoricalIndex to be unordered
- Python Pandas - Set the categories of the CategoricalIndex to be ordered
- Python Pandas - Get the minimum value from Ordered CategoricalIndex
- Python Pandas - Get the maximum value from Ordered CategoricalIndex
- Python Pandas CategoricalIndex - Get the category codes of this categorical
- Python Pandas CategoricalIndex - Map values using input correspondence like a dict
- Python Pandas - Determine if two CategoricalIndex objects contain the same elements
- Python – Reorder for consecutive elements

Advertisements