Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Set the categories of the CategoricalIndex to be unordered
To set the categories of the CategoricalIndex to be unordered, use the as_unordered() method in Pandas. This method converts an ordered categorical index to an unordered one.
Creating an Ordered CategoricalIndex
First, let's create an ordered CategoricalIndex using the ordered=True parameter ?
import pandas as pd
# Create an ordered CategoricalIndex
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"])
print("Original CategoricalIndex...")
print(catIndex)
print(f"Is ordered: {catIndex.ordered}")
Original CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Is ordered: True
Converting to Unordered Using as_unordered()
Now, use the as_unordered() method to convert the ordered categorical to unordered ?
import pandas as pd
# Create an ordered CategoricalIndex
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"])
# Convert to unordered
unordered_catIndex = catIndex.as_unordered()
print("Original (ordered):")
print(catIndex)
print(f"Is ordered: {catIndex.ordered}")
print("\nAfter as_unordered():")
print(unordered_catIndex)
print(f"Is ordered: {unordered_catIndex.ordered}")
Original (ordered): CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Is ordered: True After as_unordered(): CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category') Is ordered: False
Complete Example
Here's a comprehensive example showing the categories and the conversion process ?
import pandas as pd
# Create an ordered CategoricalIndex
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"])
# Display the original CategoricalIndex
print("CategoricalIndex...")
print(catIndex)
# Display the categories
print("\nDisplaying Categories from CategoricalIndex...")
print(catIndex.categories)
# Convert to unordered
print("\nCategoricalIndex unordered...")
print(catIndex.as_unordered())
CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') CategoricalIndex unordered... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category')
Key Points
The as_unordered() method:
- Returns a new CategoricalIndex with
ordered=False - Preserves all categories and their values
- Only changes the ordering property of the categorical data
- Does not modify the original CategoricalIndex
Conclusion
Use as_unordered() to convert an ordered CategoricalIndex to unordered. This method preserves all data and categories while removing the ordering property.
Advertisements
