How do you handle categorical data in Pandas, and how does the category dtype save memory and speed up operations?

Quick Overview

Explain how to work with categorical columns in Pandas and what the category dtype changes for memory use and speed. Tests how categorical data is stored, ordered categories, measuring memory, groupby and concat pitfalls, and when the dtype is the wrong choice.

How do you handle categorical data in Pandas, and how does the category dtype save memory and speed up operations?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How do you handle categorical data in Pandas, and what are the benefits of using the `category` dtype for memory optimization and performance? ```hint Look at what is stored per row Compare what an `object` column keeps for each row with what a column of small integer codes plus one lookup table keeps. ``` ```hint Think about combining columns Consider what happens when two categorical columns with different lists of categories are concatenated or grouped together. ``` ### What a Strong Answer Covers - How to create categorical columns, including ordered categories and setting the dtype at load time - How the dtype is stored, why that saves memory, and when the saving is large - Which operations get faster, and why - Pitfalls such as unobserved categories in `groupby`, combining mismatched categories, and assigning new values - When not to use it, and how categorical columns are encoded for modeling ### Follow-up Questions - A `groupby` on two categorical columns returns millions of rows, mostly zeros. Why, and how do you fix it? - You concatenate two DataFrames and the categorical column comes back as `object`. What happened? - How would you feed a categorical column to a linear model, and how to a gradient-boosted tree? - How would you check that converting a column actually saved memory?

Overview: Explain how to work with categorical columns in Pandas and what the category dtype changes for memory use and speed. Tests how categorical data is stored, ordered categories, measuring memory, groupby and concat pitfalls, and when the dtype is the wrong choice.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you handle categorical data in Pandas, and what are the benefits of using the category dtype for memory optimization and performance?

What a Strong Answer Covers Guidance

  • How to create categorical columns, including ordered categories and setting the dtype at load time
  • How the dtype is stored, why that saves memory, and when the saving is large
  • Which operations get faster, and why
  • Pitfalls such as unobserved categories in groupby , combining mismatched categories, and assigning new values
  • When not to use it, and how categorical columns are encoded for modeling

Follow-up Questions Guidance

  • A groupby on two categorical columns returns millions of rows, mostly zeros. Why, and how do you fix it?
  • You concatenate two DataFrames and the categorical column comes back as object . What happened?
  • How would you feed a categorical column to a linear model, and how to a gradient-boosted tree?
  • How would you check that converting a column actually saved memory?