Winsorize a pandas column at its 5th and 95th percentiles

Quick Overview

Write a pandas function that caps a numeric column at its 5th and 95th percentiles so extreme values are clipped instead of dropped. It tests linear quantile interpolation, clipping without changing the index or the input frame, and when winsorizing is a sensible way to handle outliers.

Winsorize a pandas column at its 5th and 95th percentiles

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `winsorize_col(df: pd.DataFrame, col: str) -> pd.Series` that caps values in a numeric column at its 5th and 95th percentiles instead of dropping them. Requirements: - Bounds: `lo` is the column's 0.05 quantile and `hi` is its 0.95 quantile, using pandas' default `interpolation='linear'`. - Only out-of-range values are replaced by the bounds; values inside `[lo, hi]` stay as they are. - Return a `pd.Series` with the same index as `df[col]` (no rows added or removed). Example: ```python df = pd.DataFrame({"x": [1, 2, 3, 4, 100]}) winsorize_col(df, "x") # 0 1.2 # 1 2.0 # 2 3.0 # 3 4.0 # 4 80.8 # Name: x, dtype: float64 ``` The two extremes are pulled in to the bounds and the index is unchanged. ```hint Find the cut points first Work out where the 5th and 95th percentiles fall among five sorted values with linear interpolation. Then look for a pandas method that caps values at a lower and an upper bound in one call. ``` ### Constraints & Assumptions - `col` is numeric. Missing-value handling is out of scope. - Results are compared with a floating-point tolerance, so do not round. ### What a Strong Answer Covers - How the two bounds are computed and how linear interpolation produces the example's values - Capping only the out-of-range values while keeping the length, the index and the input frame unchanged - What happens to the dtype, to constant or very short columns, and to missing values if they do appear - When winsorizing is a reasonable way to handle outliers and what it does to summary statistics ### Follow-up Questions - How would you make the percentile bounds configurable? - How would you winsorize many columns at once? - In a model pipeline, which data should the bounds come from, and how do you apply them to new data? - What does winsorizing do to the mean and variance of a column, and when would you handle outliers another way?

Overview: Write a pandas function that caps a numeric column at its 5th and 95th percentiles so extreme values are clipped instead of dropped. It tests linear quantile interpolation, clipping without changing the index or the input frame, and when winsorizing is a sensible way to handle outliers.

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

Implement winsorize_col(df: pd.DataFrame, col: str) -> pd.Series that caps values in a numeric column at its 5th and 95th percentiles instead of dropping them.

Requirements:

  • Bounds: lo is the column's 0.05 quantile and hi is its 0.95 quantile, using pandas' default interpolation='linear' .
  • Only out-of-range values are replaced by the bounds; values inside [lo, hi] stay as they are.
  • Return a pd.Series with the same index as df[col] (no rows added or removed).

Example:

df = pd.DataFrame({"x": [1, 2, 3, 4, 100]})
winsorize_col(df, "x")
# 0     1.2
# 1     2.0
# 2     3.0
# 3     4.0
# 4    80.8
# Name: x, dtype: float64

The two extremes are pulled in to the bounds and the index is unchanged.

Constraints & Assumptions

  • col is numeric. Missing-value handling is out of scope.
  • Results are compared with a floating-point tolerance, so do not round.

What a Strong Answer Covers Guidance

  • How the two bounds are computed and how linear interpolation produces the example's values
  • Capping only the out-of-range values while keeping the length, the index and the input frame unchanged
  • What happens to the dtype, to constant or very short columns, and to missing values if they do appear
  • When winsorizing is a reasonable way to handle outliers and what it does to summary statistics

Follow-up Questions Guidance

  • How would you make the percentile bounds configurable?
  • How would you winsorize many columns at once?
  • In a model pipeline, which data should the bounds come from, and how do you apply them to new data?
  • What does winsorizing do to the mean and variance of a column, and when would you handle outliers another way?