Build a Weight of Evidence (WoE) and Information Value Table with Quantile Bins in Pandas

Quick Overview

Build a Weight of Evidence and Information Value table in pandas by cutting a feature into quantile bins and comparing each bin's share of events and non-events. It tests qcut, grouped counts, exact output dtypes, and how WoE and IV are used to screen features for credit scorecards.

Build a Weight of Evidence (WoE) and Information Value Table with Quantile Bins in Pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `woe_iv_table(df, feature, target, q=4)`. Cut `df[feature]` into `q` quantile bins with `pd.qcut(..., duplicates='drop')` and compute the Weight of Evidence and the Information Value contribution of each bin, treating `target == 1` as an event and `target == 0` as a non-event. The returned DataFrame must meet these requirements: - Columns exactly `['bin', 'n', 'events', 'non_events', 'pct_events', 'pct_non_events', 'woe', 'iv_contrib']`. - `bin` is the stringified interval (object dtype). `n`, `events` and `non_events` are `int64`. - `pct_events = events / total events` and `pct_non_events = non_events / total non-events`, both `float64`. - `woe = ln(pct_non_events / pct_events)` and `iv_contrib = (pct_non_events - pct_events) * woe`. - Rows are in ascending bin order, with `reset_index(drop=True)`. Example with `q=4`: ```text feature: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 target: 1 1 1 0 1 1 0 0 1 0 0 0 0 0 0 1 ``` Expected result (floats rounded to six decimals): ```text bin n events non_events pct_events pct_non_events woe iv_contrib 0 (0.999, 4.75] 4 3 1 0.428571 0.111111 -1.349927 0.428548 1 (4.75, 8.5] 4 2 2 0.285714 0.222222 -0.251314 0.015956 2 (8.5, 12.25] 4 1 3 0.142857 0.333333 0.847298 0.161390 3 (12.25, 16.0] 4 1 3 0.142857 0.333333 0.847298 0.161390 ``` There are 7 events and 9 non-events in total. The first bin holds 3 of the 7 events and 1 of the 9 non-events, so its WoE is ln((1/9) / (3/7)) = ln(7/27), about -1.349927. Summing `iv_contrib` gives the feature's Information Value, about 0.767285. ```hint Keep the bin order Group on the categorical that `pd.qcut` returns and turn the intervals into strings only at the end. Sorted as strings, `'(12.25, 16.0]'` comes before `'(4.75, 8.5]'`. ``` ```hint Which denominator Each percentage divides a bin's count by that class's total over all bins. The event rate inside a bin is a different quantity. ``` ### Constraints & Assumptions - `target` contains only 0 and 1. - Every bin in the test data has at least one event and one non-event, so no zero-division or log-of-zero handling is needed. - With `duplicates='drop'`, a feature with many tied values can produce fewer than `q` bins. Return one row per bin that `qcut` produces. ### What a Strong Answer Covers - Binning with `pd.qcut` and keeping the intervals ordered through the aggregation. - Counting rows, events and non-events per bin with the exact int64 dtypes. - Computing each class's share across bins, then WoE and the IV contribution with the stated sign convention. - Meeting the output contract: exact column order, object `bin` strings, ascending rows and a reset index. - What WoE and IV mean for screening a feature, and where the calculation breaks (empty classes in a bin, tied values, missing feature values). ### Follow-up Questions - How would you keep WoE finite when a bin has zero events or zero non-events? - How would you enforce monotonic WoE across bins? - How do you read the total Information Value, and what would make you distrust a very high value?

Overview: Build a Weight of Evidence and Information Value table in pandas by cutting a feature into quantile bins and comparing each bin's share of events and non-events. It tests qcut, grouped counts, exact output dtypes, and how WoE and IV are used to screen features for credit scorecards.

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

Implement woe_iv_table(df, feature, target, q=4). Cut df[feature] into q quantile bins with pd.qcut(..., duplicates='drop') and compute the Weight of Evidence and the Information Value contribution of each bin, treating target == 1 as an event and target == 0 as a non-event.

The returned DataFrame must meet these requirements:

  • Columns exactly ['bin', 'n', 'events', 'non_events', 'pct_events', 'pct_non_events', 'woe', 'iv_contrib'] .
  • bin is the stringified interval (object dtype). n , events and non_events are int64 .
  • pct_events = events / total events and pct_non_events = non_events / total non-events , both float64 .
  • woe = ln(pct_non_events / pct_events) and iv_contrib = (pct_non_events - pct_events) * woe .
  • Rows are in ascending bin order, with reset_index(drop=True) .

Example with q=4:

feature: 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16
target:  1  1  1  0  1  1  0  0  1   0   0   0   0   0   0   1

Expected result (floats rounded to six decimals):

             bin  n  events  non_events  pct_events  pct_non_events       woe  iv_contrib
0  (0.999, 4.75]  4       3           1    0.428571        0.111111 -1.349927    0.428548
1    (4.75, 8.5]  4       2           2    0.285714        0.222222 -0.251314    0.015956
2   (8.5, 12.25]  4       1           3    0.142857        0.333333  0.847298    0.161390
3  (12.25, 16.0]  4       1           3    0.142857        0.333333  0.847298    0.161390

There are 7 events and 9 non-events in total. The first bin holds 3 of the 7 events and 1 of the 9 non-events, so its WoE is ln((1/9) / (3/7)) = ln(7/27), about -1.349927. Summing iv_contrib gives the feature's Information Value, about 0.767285.

Constraints & Assumptions

  • target contains only 0 and 1.
  • Every bin in the test data has at least one event and one non-event, so no zero-division or log-of-zero handling is needed.
  • With duplicates='drop' , a feature with many tied values can produce fewer than q bins. Return one row per bin that qcut produces.

What a Strong Answer Covers Guidance

  • Binning with pd.qcut and keeping the intervals ordered through the aggregation.
  • Counting rows, events and non-events per bin with the exact int64 dtypes.
  • Computing each class's share across bins, then WoE and the IV contribution with the stated sign convention.
  • Meeting the output contract: exact column order, object bin strings, ascending rows and a reset index.
  • What WoE and IV mean for screening a feature, and where the calculation breaks (empty classes in a bin, tied values, missing feature values).

Follow-up Questions Guidance

  • How would you keep WoE finite when a bin has zero events or zero non-events?
  • How would you enforce monotonic WoE across bins?
  • How do you read the total Information Value, and what would make you distrust a very high value?