One-hot encode a size column into k-1 dummies with drop_first in pandas

Quick Overview

Write a pandas function that one-hot encodes a size column into k-1 boolean dummy columns with drop_first and replaces the original column. It tests which level get_dummies drops, how to read the baseline category, and why the dummy variable trap matters for linear models.

One-hot encode a size column into k-1 dummies with drop_first in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `dummies_drop_first(df)` that one-hot encodes the `'size'` column (values `'S'`, `'M'`, `'L'`) into k-1 columns, replacing `'size'` and keeping other columns (for example `'id'`). The function must meet these requirements: - Use `pd.get_dummies` on `'size'` with `drop_first=True`. - Categories sort alphabetically, so `'size_L'` is dropped, leaving `'size_M'` and `'size_S'` (boolean dtype). - Remove `'size'` and place `'id'` to the left of the dummies. - Preserve the original index. Example: ```python df = pd.DataFrame({'id': [1, 2, 3], 'size': ['S', 'M', 'L']}) dummies_drop_first(df) # id size_M size_S # 0 1 False True # 1 2 True False # 2 3 False False ``` ```hint Reading the output Look at the last row of the example, where every dummy is False. Work out what that row means, and what else could produce the same row. ``` ### What a Strong Answer Covers - Encoding only `size` with `drop_first=True` and replacing the original column - Which level is dropped and why, given the order `get_dummies` uses - What an all-False row means once a level is dropped - Keeping `id` to the left while preserving the index and the boolean dtype ### Follow-up Questions - Why drop a column at all, and for which models would you keep all k dummies? - How would you make `'S'` the baseline category instead of `'L'`? - What goes wrong if you run this function separately on a test set that has no `'L'` rows, or on data with missing sizes? - Would an ordinal encoding suit `'S'`, `'M'`, `'L'` better than one-hot columns?

Overview: Write a pandas function that one-hot encodes a size column into k-1 boolean dummy columns with drop_first and replaces the original column. It tests which level get_dummies drops, how to read the baseline category, and why the dummy variable trap matters for linear models.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write dummies_drop_first(df) that one-hot encodes the 'size' column (values 'S', 'M', 'L') into k-1 columns, replacing 'size' and keeping other columns (for example 'id').

The function must meet these requirements:

  • Use pd.get_dummies on 'size' with drop_first=True .
  • Categories sort alphabetically, so 'size_L' is dropped, leaving 'size_M' and 'size_S' (boolean dtype).
  • Remove 'size' and place 'id' to the left of the dummies.
  • Preserve the original index.

Example:

df = pd.DataFrame({'id': [1, 2, 3], 'size': ['S', 'M', 'L']})
dummies_drop_first(df)
#    id  size_M  size_S
# 0   1   False    True
# 1   2    True   False
# 2   3   False   False

What a Strong Answer Covers Guidance

  • Encoding only size with drop_first=True and replacing the original column
  • Which level is dropped and why, given the order get_dummies uses
  • What an all-False row means once a level is dropped
  • Keeping id to the left while preserving the index and the boolean dtype

Follow-up Questions Guidance

  • Why drop a column at all, and for which models would you keep all k dummies?
  • How would you make 'S' the baseline category instead of 'L' ?
  • What goes wrong if you run this function separately on a test set that has no 'L' rows, or on data with missing sizes?
  • Would an ordinal encoding suit 'S' , 'M' , 'L' better than one-hot columns?