
- 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 - Determine if two CategoricalIndex objects contain the same elements
To determine if two CategoricalIndex objects contain the same elements, use the equals() method. At first, import the required libraries −
import pandas as pd
Set the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter. Create two CategoricalIndex objects −
catIndex1 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) catIndex2 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
Check both the CategoricalIndex objects for equality −
print("\nCheck both the CategoricalIndex objects for equality...\n",catIndex1.equals(catIndex2))
Example
Following is the code −
import pandas as pd # Set the categories for the categorical using the "categories" parameter # Treat the categorical as ordered using the "ordered" parameter # Create two CategoricalIndex objects catIndex1 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) catIndex2 = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) # Display the CategoricalIndex objects print("CategoricalIndex1...\n",catIndex1) print("\nCategoricalIndex2...\n",catIndex2) # Check both the CategoricalIndex objects for equality print("\nCheck both the CategoricalIndex objects for equality...\n",catIndex1.equals(catIndex2))
Output
This will produce the following output −
CategoricalIndex1... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') CategoricalIndex2... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Check both the CategoricalIndex objects for equality... True
- Related Articles
- Python Pandas - Determine if two Index objects are equal
- Python Pandas - Determine if two Index objects with opposite orders are equal or not
- Python Pandas CategoricalIndex - Rename categories
- Python Pandas CategoricalIndex - Reorder categories
- Python Pandas CategoricalIndex - Add new categories
- Simple way to find if two different lists contain exactly the same elements in Java
- Python Pandas – Check if two Dataframes are exactly same
- Python Pandas - Remove the specified categories from CategoricalIndex
- Python Pandas CategoricalIndex - Rename categories with lambda
- Python Pandas - Check elementwise if the Intervals contain the value
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python Pandas - Get the minimum value from Ordered CategoricalIndex
- Python Pandas - Get the maximum value from Ordered CategoricalIndex
- Python Pandas CategoricalIndex - Get the categories of this categorical
- Python Pandas - Form the intersection of two Index objects

Advertisements