Categorical Data

Pandas Basics

1 min read

Published Sep 29 2025, updated Aug 17 2026


21
0
0
0

PandasPython

What is Categorical Data?

  • A Pandas data type that represents discrete values with limited categories.
  • Examples:
    • Gender: "Male", "Female"
    • Size: "S", "M", "L", "XL"
    • Days of week: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"




Why Use Categorical Data?

  • Memory efficiency: stores values as integers under the hood, not full strings.
  • Performance: comparisons and groupby operations are faster.
  • Explicit ordering: you can specify an order (Small < Medium < Large).





Creating Categorical Data

From a Series:

import pandas as pdsizes = pd.Series(["S", "M", "L", "S", "M"])cat_sizes = sizes.astype("category")print(cat_sizes)

Output:

0    S1    M2    L3    S4    Mdtype: categoryCategories (3, object): ['L', 'M', 'S']


Categories and Order:

sizes = pd.Series(["S", "M", "L", "S", "M"])cat_sizes = pd.Categorical(sizes, categories=["S", "M", "L"], ordered=True)print(cat_sizes)

Now "S" < "M" < "L" is respected in comparisons.






Comparisons

cat_sizes = pd.Categorical(["S", "M", "L"], categories=["S","M","L"], ordered=True)# True (S < L)print(cat_sizes.codes[0] < cat_sizes.codes[2])

Need to be ordered for the above to work.






Common Operations

Check Categories:

cat_sizes.categories

Index(['S', 'M', 'L'], dtype='object')



Change Categories:

cat_sizes = cat_sizes.rename_categories(["Small", "Medium", "Large"])


Reorder Categories:

cat_sizes = cat_sizes.reorder_categories(["Large", "Medium", "Small"], ordered=True)


Add or Remove Categories:

cat_sizes = cat_sizes.add_categories(["XL"])cat_sizes = cat_sizes.remove_categories(["Small"])





Integration with DataFrames

df = pd.DataFrame({    "Size": ["S", "M", "L", "S", "M"]})df["Size"] = df["Size"].astype("category")


.cat methods:

  • .cat.categories → list categories
  • .cat.codes → underlying integer codes
  • .cat.set_categories([...]) → change categories
  • .cat.remove_unused_categories() → drop unused ones

Example:

df["Size"].cat.codes

Output:

0    01    12    23    04    1dtype: int8
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Pandas Basics | Categorical Data | SimpleSteps.guide