We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Category Type

If a column repeats the same small set of labels over and over, plain strings are wasteful. Pandas' category type stores each label once, then uses compact integer codes under the hood. This saves memory and speeds up operations like grouping, sorting, and filtering. It's great for things like:

  • membership_tier: "free", "pro", "enterprise"
  • region: "West", "Midwest", "Northeast", "South"
  • ticket_status: "open", "pending", "closed"

You can convert a column to category with astype():

df["region"] = df["region"].astype("category")

Grouping by Category

If you use groupby() on a category column, observed=True keeps the result focused on categories that actually appear in the data:

df.groupby("region", observed=True)["sales_lead"].count()

Assignment

Complete the get_region_distribution function. It accepts a DataFrame with fixed region and sales_lead columns, converts the region column to a category type, and returns a DataFrame with one row per observed region and its lead count.