</>
Skip to content
Pandas lessons (39/42)

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

  1. Create categorical data
  2. View categories
  3. Set custom categories
  4. 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.