- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas CategoricalIndex - Get the categories of this categorical
To get the categories of this categorical, use the categories property of the CategoricalIndex 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 (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 Categorical Index −
print("Categorical Index...\n",catIndex)
Get the categories −
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
Example
Following is the code −
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 Categorical Index print("Categorical Index...\n",catIndex) # Get the categories print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
Output
This will produce the following output −
Categorical Index... 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')
Advertisements