Pandas — Categorical Data
Create categorical
import pandas as pd
df = pd.DataFrame({'grade': ['A', 'B', 'C', 'A', 'B']})
df['grade'] = df['grade'].astype('category')
Categories
print(df['grade'].cat.categories)
print(df['grade'].cat.codes)
Set categories
df['grade'] = df['grade'].cat.set_categories(['A', 'B', 'C', 'D'])
Add categories
df['grade'] = df['grade'].cat.add_categories(['D'])
Remove categories
df['grade'] = df['grade'].cat.remove_categories(['D'])
Mini Practice
- Create categorical data
- View categories
- Set custom categories
- Add/remove categories
Up Next
Continue with Visualization - Plotting data.
Related Topics
Frequently Asked Questions about Categorical Data
What is Categorical Data in Pandas?
Categorical Data is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Categorical Data?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Categorical Data.
Why is Categorical Data important in Pandas?
Categorical Data is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.