Categorical Target Rate and Lift Table

Quick Overview

Write a pandas function that builds a per-category table of row counts, target rates and lift against the overall base rate for a binary target. It tests groupby aggregation, exact dtypes and sort order, and safe handling of a zero base rate.

Categorical Target Rate and Lift Table

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `target_rate_by_category(df, cat_col, target)` for a pandas DataFrame `df`, where `target` names an integer 0/1 column and `cat_col` names a categorical or object column. For each level of `cat_col`, return the row count, the target rate, and the lift versus the overall base rate. The result must meet these requirements: - Columns are exactly `['category', 'n', 'target_rate', 'lift']`. - `n` is the number of rows in the level (`int64`). `target_rate` is the mean of the target within the level (`float64`). - `lift` is `target_rate / base_rate`, where `base_rate = df[target].mean()` over the whole frame (`float64`). - If `base_rate == 0`, every `lift` is `np.nan`, with no `ZeroDivisionError` and no `inf`. - Rows are sorted by `target_rate` descending, then `category` ascending, followed by `reset_index(drop=True)`. The function is graded with `assert_frame_equal` against a fixed frame, so dtypes and column order must match exactly. ```hint Dtypes and the zero case One `groupby` gives both the count and the mean of a 0/1 column. Check the dtype each aggregation returns, and decide what the lift should be before you divide by a base rate that can be zero. ``` ### What a Strong Answer Covers - Computes the base rate once over the whole frame, before grouping - Gets the count and rate of every level from one grouped pass and moves the group key into a `category` column - Handles a zero base rate explicitly so every lift is NaN - Matches the exact column order, dtypes, sort order and a clean 0 to k-1 index - States the complexity and edge cases such as an empty frame, missing category values and unused categorical levels ### Follow-up Questions - How would you add a smoothed rate to stabilize levels with very few rows? - How would you attach a confidence interval to each `target_rate`? - Rows with a missing `cat_col` value drop out of the groupby. How would you keep them, and how does that interact with the base rate?

Overview: Write a pandas function that builds a per-category table of row counts, target rates and lift against the overall base rate for a binary target. It tests groupby aggregation, exact dtypes and sort order, and safe handling of a zero base rate.

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

Implement target_rate_by_category(df, cat_col, target) for a pandas DataFrame df, where target names an integer 0/1 column and cat_col names a categorical or object column. For each level of cat_col, return the row count, the target rate, and the lift versus the overall base rate.

The result must meet these requirements:

  • Columns are exactly ['category', 'n', 'target_rate', 'lift'] .
  • n is the number of rows in the level ( int64 ). target_rate is the mean of the target within the level ( float64 ).
  • lift is target_rate / base_rate , where base_rate = df[target].mean() over the whole frame ( float64 ).
  • If base_rate == 0 , every lift is np.nan , with no ZeroDivisionError and no inf .
  • Rows are sorted by target_rate descending, then category ascending, followed by reset_index(drop=True) .

The function is graded with assert_frame_equal against a fixed frame, so dtypes and column order must match exactly.

What a Strong Answer Covers Guidance

  • Computes the base rate once over the whole frame, before grouping
  • Gets the count and rate of every level from one grouped pass and moves the group key into a category column
  • Handles a zero base rate explicitly so every lift is NaN
  • Matches the exact column order, dtypes, sort order and a clean 0 to k-1 index
  • States the complexity and edge cases such as an empty frame, missing category values and unused categorical levels

Follow-up Questions Guidance

  • How would you add a smoothed rate to stabilize levels with very few rows?
  • How would you attach a confidence interval to each target_rate ?
  • Rows with a missing cat_col value drop out of the groupby. How would you keep them, and how does that interact with the base rate?