Compute box plot statistics (quartiles, fences, whiskers, outliers) for a pandas column

Quick Overview

Write a pandas function that returns the numbers behind a box plot: quartiles, IQR, Tukey fences, whisker ends and outlier counts. It tests linear quantile interpolation, the difference between fences and whiskers, strict outlier bounds and consistent handling of missing values.

Compute box plot statistics (quartiles, fences, whiskers, outliers) for a pandas column

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `boxplot_stats(df, col)` that returns the box plot statistics of the numeric column `col` (excluding NaNs) as a pandas `Series` with this exact index order: `['q1', 'median', 'q3', 'iqr', 'lower_fence', 'upper_fence', 'lower_whisker', 'upper_whisker', 'n_outliers', 'n_total']`. Requirements: - `q1`, `median` and `q3` come from `Series.quantile` at 0.25, 0.5 and 0.75 with the default `interpolation='linear'`, and `iqr = q3 - q1`. - `lower_fence = q1 - 1.5*iqr` and `upper_fence = q3 + 1.5*iqr`. - `lower_whisker` is the smallest observed value `>= lower_fence`, and `upper_whisker` is the largest observed value `<= upper_fence`. Both are real data points, not the fences. - `n_outliers` counts values strictly outside the fences, and `n_total` counts the non-null values. - The Series has dtype `float64`. The two counts are whole numbers stored as floats (1.0 and 10.0 in the example below). Example: ```python df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 100]}) boxplot_stats(df, 'x') # q1 3.25 # median 5.50 # q3 7.75 # iqr 4.50 # lower_fence -3.50 # upper_fence 14.50 # lower_whisker 1.00 # smallest value >= -3.50 # upper_whisker 9.00 # largest value <= 14.50, not the fence # n_outliers 1.00 # 100 lies outside the fences # n_total 10.00 ``` ```hint Fences and whiskers The fences are thresholds computed from the quartiles. Ask which observed values are allowed to be the end of a whisker. ``` ### Constraints & Assumptions - `col` is numeric and has at least one non-null value. - NaNs are excluded from every statistic, including `n_total`. - Quartiles use pandas' default linear interpolation. ### What a Strong Answer Covers - Dropping NaNs once so every statistic is computed on the same values - Computing the quartiles with linear interpolation and Tukey's 1.5 IQR fences - Choosing the whiskers from observed values inside the fences - Counting outliers with strict inequalities and returning the exact index order as one `float64` Series ### Follow-up Questions - How would you also return the list of outlier values, not just `n_outliers`? - Why do whiskers land on real data points instead of on the fences themselves? - How would you compute the same statistics for every group in a grouped DataFrame?

Overview: Write a pandas function that returns the numbers behind a box plot: quartiles, IQR, Tukey fences, whisker ends and outlier counts. It tests linear quantile interpolation, the difference between fences and whiskers, strict outlier bounds and consistent handling of missing values.

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

Write boxplot_stats(df, col) that returns the box plot statistics of the numeric column col (excluding NaNs) as a pandas Series with this exact index order: ['q1', 'median', 'q3', 'iqr', 'lower_fence', 'upper_fence', 'lower_whisker', 'upper_whisker', 'n_outliers', 'n_total'].

Requirements:

  • q1 , median and q3 come from Series.quantile at 0.25, 0.5 and 0.75 with the default interpolation='linear' , and iqr = q3 - q1 .
  • lower_fence = q1 - 1.5*iqr and upper_fence = q3 + 1.5*iqr .
  • lower_whisker is the smallest observed value >= lower_fence , and upper_whisker is the largest observed value <= upper_fence . Both are real data points, not the fences.
  • n_outliers counts values strictly outside the fences, and n_total counts the non-null values.
  • The Series has dtype float64 . The two counts are whole numbers stored as floats (1.0 and 10.0 in the example below).

Example:

df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 100]})
boxplot_stats(df, 'x')
# q1               3.25
# median           5.50
# q3               7.75
# iqr              4.50
# lower_fence     -3.50
# upper_fence     14.50
# lower_whisker    1.00   # smallest value >= -3.50
# upper_whisker    9.00   # largest value <= 14.50, not the fence
# n_outliers       1.00   # 100 lies outside the fences
# n_total         10.00

Constraints & Assumptions

  • col is numeric and has at least one non-null value.
  • NaNs are excluded from every statistic, including n_total .
  • Quartiles use pandas' default linear interpolation.

What a Strong Answer Covers Guidance

  • Dropping NaNs once so every statistic is computed on the same values
  • Computing the quartiles with linear interpolation and Tukey's 1.5 IQR fences
  • Choosing the whiskers from observed values inside the fences
  • Counting outliers with strict inequalities and returning the exact index order as one float64 Series

Follow-up Questions Guidance

  • How would you also return the list of outlier values, not just n_outliers ?
  • Why do whiskers land on real data points instead of on the fences themselves?
  • How would you compute the same statistics for every group in a grouped DataFrame?