Build a quantile-binned target rate and lift table in pandas

Quick Overview

Write a pandas function that cuts a numeric feature into quantile bins and reports the row count, target rate and lift for each bin. It tests qcut tie handling, how missing values affect counts and the base rate, groupby ordering, and exact dtype matching under assert_frame_equal.

Build a quantile-binned target rate and lift table in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `target_rate_by_bin(df, feature, target, q=4)`. Cut `df[feature]` into `q` quantile bins with `pd.qcut(..., duplicates='drop')` and summarize the 0/1 target in each bin. The result is graded with `assert_frame_equal`, so column names, dtypes and row order must match exactly: - The columns are exactly `['bin', 'n', 'target_rate', 'lift']`. - `bin` is the interval as a string (object dtype), for example `'(0.999, 3.0]'`. - `n` is the row count of the bin (`int64`) and `target_rate` is the mean of the target in the bin (`float64`). - `lift = target_rate / base_rate`, with `base_rate = df[target].mean()` over the whole frame. - Rows are in ascending bin order (not by rate), followed by `reset_index(drop=True)`. ```hint Where the order comes from `qcut` returns an ordered categorical. Think about what grouping by it does to the row order, and to bins that end up with no rows. ``` ```hint Check the types `assert_frame_equal` compares dtypes as well as values. Look at what type the grouped index and the counts come back as before you build the output frame. ``` ### Constraints & Assumptions - The target column holds 0/1 integers. - The feature is numeric and may contain NaN. Rows with a NaN feature are dropped by `qcut` and not counted in any bin, but they are still part of the whole frame used for `base_rate`. - Ties must not raise. Getting fewer than `q` bins is an expected result. - A bin that receives no rows does not appear in the output. ### What a Strong Answer Covers - Computing the base rate on the full frame before any rows fall out - Using `duplicates='drop'` so repeated quantile edges collapse instead of raising - Keeping ascending interval order and keeping NaN-feature rows out of the counts - Matching column names, column order and dtypes (`object`, `int64`, `float64`) exactly - Handling a base rate of zero without a silent division problem ### Follow-up Questions - How would you handle a feature with very few distinct values, for example one that is mostly a single value? - How would you add a confidence measure to each bin's `target_rate`? - How would you report the rows with a missing feature as their own row in the table?

Overview: Write a pandas function that cuts a numeric feature into quantile bins and reports the row count, target rate and lift for each bin. It tests qcut tie handling, how missing values affect counts and the base rate, groupby ordering, and exact dtype matching under assert_frame_equal.

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

Implement target_rate_by_bin(df, feature, target, q=4). Cut df[feature] into q quantile bins with pd.qcut(..., duplicates='drop') and summarize the 0/1 target in each bin.

The result is graded with assert_frame_equal, so column names, dtypes and row order must match exactly:

  • The columns are exactly ['bin', 'n', 'target_rate', 'lift'] .
  • bin is the interval as a string (object dtype), for example '(0.999, 3.0]' .
  • n is the row count of the bin ( int64 ) and target_rate is the mean of the target in the bin ( float64 ).
  • lift = target_rate / base_rate , with base_rate = df[target].mean() over the whole frame.
  • Rows are in ascending bin order (not by rate), followed by reset_index(drop=True) .

Constraints & Assumptions

  • The target column holds 0/1 integers.
  • The feature is numeric and may contain NaN. Rows with a NaN feature are dropped by qcut and not counted in any bin, but they are still part of the whole frame used for base_rate .
  • Ties must not raise. Getting fewer than q bins is an expected result.
  • A bin that receives no rows does not appear in the output.

What a Strong Answer Covers Guidance

  • Computing the base rate on the full frame before any rows fall out
  • Using duplicates='drop' so repeated quantile edges collapse instead of raising
  • Keeping ascending interval order and keeping NaN-feature rows out of the counts
  • Matching column names, column order and dtypes ( object , int64 , float64 ) exactly
  • Handling a base rate of zero without a silent division problem

Follow-up Questions Guidance

  • How would you handle a feature with very few distinct values, for example one that is mostly a single value?
  • How would you add a confidence measure to each bin's target_rate ?
  • How would you report the rows with a missing feature as their own row in the table?