One-hot encode a categorical predictor for linear regression in pandas

Quick Overview

Write a pandas function that one-hot encodes a city column for linear regression, dropping a reference level and returning integer dummy columns next to a numeric feature. It tests get_dummies behavior, the dummy variable trap, index and dtype handling, and keeping a stable column schema for new data.

One-hot encode a categorical predictor for linear regression in pandas

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `encode_for_regression(df)`. `df` has exactly two columns: `x` (float) and `city` (object, with values from `'Berlin'`, `'Hamburg'` and `'Munich'`). Return a new DataFrame from `pd.get_dummies` on the whole frame, dropping the reference level, with integer dummies. ```python def encode_for_regression(df: pd.DataFrame) -> pd.DataFrame: ... ``` Requirements: - Use `pd.get_dummies` with `drop_first=True` and `dtype=int`, so `'Berlin'` is dropped as the reference. - The columns are exactly `['x', 'city_Hamburg', 'city_Munich']`. - `x` stays `float64`, and the dummy columns are `int64` (not `bool`). - Preserve the input index and do not mutate the input. Example: ```python df = pd.DataFrame({'x': [1.0, 2.0, 3.0], 'city': ['Berlin', 'Hamburg', 'Munich']}) encode_for_regression(df) # x city_Hamburg city_Munich # 0 1.0 0 0 # 1 2.0 1 0 # 2 3.0 0 1 ``` ```hint What get_dummies does to a whole frame Check which columns `pd.get_dummies` encodes when you pass it a DataFrame, how it orders the category levels, and what dtype it gives the dummies by default. ``` ### Constraints & Assumptions - `df` has exactly the columns `x` (float64) and `city` (object). - `city` takes values from `'Berlin'`, `'Hamburg'` and `'Munich'`. ### What a Strong Answer Covers - Encoding only the categorical column while `x` passes through first and stays float64. - Which level becomes the reference, and how the dummy names and integer dtype come out. - Index preservation and leaving the input unchanged. - Why one level is dropped when the model has an intercept. - What happens to the output columns when a frame lacks a city, has a missing value or has an unexpected one. ### Follow-up Questions - Why drop the reference level instead of keeping all three dummies? - How would you make sure new data at inference time, including unseen categories, gets the same columns? - How do you interpret the coefficient on `city_Munich` in a linear regression of `y` on these columns?

Overview: Write a pandas function that one-hot encodes a city column for linear regression, dropping a reference level and returning integer dummy columns next to a numeric feature. It tests get_dummies behavior, the dummy variable trap, index and dtype handling, and keeping a stable column schema for new data.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement encode_for_regression(df). df has exactly two columns: x (float) and city (object, with values from 'Berlin', 'Hamburg' and 'Munich'). Return a new DataFrame from pd.get_dummies on the whole frame, dropping the reference level, with integer dummies.

def encode_for_regression(df: pd.DataFrame) -> pd.DataFrame:
    ...

Requirements:

  • Use pd.get_dummies with drop_first=True and dtype=int , so 'Berlin' is dropped as the reference.
  • The columns are exactly ['x', 'city_Hamburg', 'city_Munich'] .
  • x stays float64 , and the dummy columns are int64 (not bool ).
  • Preserve the input index and do not mutate the input.

Example:

df = pd.DataFrame({'x': [1.0, 2.0, 3.0], 'city': ['Berlin', 'Hamburg', 'Munich']})
encode_for_regression(df)
#      x  city_Hamburg  city_Munich
# 0  1.0             0            0
# 1  2.0             1            0
# 2  3.0             0            1

Constraints & Assumptions

  • df has exactly the columns x (float64) and city (object).
  • city takes values from 'Berlin' , 'Hamburg' and 'Munich' .

What a Strong Answer Covers Guidance

  • Encoding only the categorical column while x passes through first and stays float64.
  • Which level becomes the reference, and how the dummy names and integer dtype come out.
  • Index preservation and leaving the input unchanged.
  • Why one level is dropped when the model has an intercept.
  • What happens to the output columns when a frame lacks a city, has a missing value or has an unexpected one.

Follow-up Questions Guidance

  • Why drop the reference level instead of keeping all three dummies?
  • How would you make sure new data at inference time, including unseen categories, gets the same columns?
  • How do you interpret the coefficient on city_Munich in a linear regression of y on these columns?