Flag unreliable segments by sample size with a pandas group-by

Quick Overview

Write a pandas function that groups rows by segment and reports each segment's row count, mean metric and whether it meets a minimum sample size. It tests group-by aggregation, exact output dtypes and sorting, and judging when a segment average is too small to trust.

Flag unreliable segments by sample size with a pandas group-by

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `unreliable_segments(df, seg, metric, min_n)`. It groups `df` by the column `seg` and returns, for every segment, its row count, the mean of `metric`, and whether the segment meets the minimum count `min_n`. The report must follow these rules: - Columns are `segment` (`object`), `n` (`int64`), `segment_mean` (`float64`) and `reliable` (a real `bool` dtype, not `object`). - `reliable` is `True` if and only if `n >= min_n`. - Every segment is included, reliable or not. - Rows are sorted by `n` ascending, ties broken by `segment` ascending, and the index is reset with `reset_index(drop=True)`. - The result is graded with `assert_frame_equal` using `check_dtype=True`. Example with `min_n=5`: ```python df = pd.DataFrame({ "region": ["C", "A", "C", "B", "C", "C", "B", "C", "C", "C", "C"], "spend": [15, 10, 25, 10, 15, 25, 15, 15, 25, 15, 25], }) unreliable_segments(df, "region", "spend", 5) ``` ```text segment n segment_mean reliable 0 A 1 10.0 False 1 B 2 12.5 False 2 C 8 20.0 True ``` ```hint Watch the dtypes `check_dtype=True` compares the dtype of every column. Check what your group key and your flag column actually come out as after the aggregation, not only their values. ``` ### Constraints & Assumptions - `seg` has no missing values, and its values can be compared with each other for sorting. - The segments are the values that occur in `df`. Unused categories of a categorical `seg` column are not reported. - `min_n` is an integer. - For the main task, `metric` is numeric with no missing values. The follow-ups cover missing values. ### What a Strong Answer Covers - One row per segment with its row count and mean, built in a single group-by, with no segment filtered out. - The `reliable` flag as a vectorized comparison with the inclusive `>=` boundary. - The exact dtype of all four columns, including turning the group key into a plain `object` column and keeping `reliable` as `bool`. - The two-key sort and the reset index the grader compares. - Why a row-count threshold is only a rough proxy for how much a segment mean can be trusted. ### Follow-up Questions - How would you use a confidence interval width instead of a hard `min_n` flag? - What if `metric` contains NaNs? - Many segments fail the threshold. How would you still report something useful for them without discarding their data?

Overview: Write a pandas function that groups rows by segment and reports each segment's row count, mean metric and whether it meets a minimum sample size. It tests group-by aggregation, exact output dtypes and sorting, and judging when a segment average is too small to trust.

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

Write unreliable_segments(df, seg, metric, min_n). It groups df by the column seg and returns, for every segment, its row count, the mean of metric, and whether the segment meets the minimum count min_n.

The report must follow these rules:

  • Columns are segment ( object ), n ( int64 ), segment_mean ( float64 ) and reliable (a real bool dtype, not object ).
  • reliable is True if and only if n >= min_n .
  • Every segment is included, reliable or not.
  • Rows are sorted by n ascending, ties broken by segment ascending, and the index is reset with reset_index(drop=True) .
  • The result is graded with assert_frame_equal using check_dtype=True .

Example with min_n=5:

df = pd.DataFrame({
    "region": ["C", "A", "C", "B", "C", "C", "B", "C", "C", "C", "C"],
    "spend":  [15, 10, 25, 10, 15, 25, 15, 15, 25, 15, 25],
})
unreliable_segments(df, "region", "spend", 5)
  segment  n  segment_mean  reliable
0       A  1          10.0     False
1       B  2          12.5     False
2       C  8          20.0      True

Constraints & Assumptions

  • seg has no missing values, and its values can be compared with each other for sorting.
  • The segments are the values that occur in df . Unused categories of a categorical seg column are not reported.
  • min_n is an integer.
  • For the main task, metric is numeric with no missing values. The follow-ups cover missing values.

What a Strong Answer Covers Guidance

  • One row per segment with its row count and mean, built in a single group-by, with no segment filtered out.
  • The reliable flag as a vectorized comparison with the inclusive >= boundary.
  • The exact dtype of all four columns, including turning the group key into a plain object column and keeping reliable as bool .
  • The two-key sort and the reset index the grader compares.
  • Why a row-count threshold is only a rough proxy for how much a segment mean can be trusted.

Follow-up Questions Guidance

  • How would you use a confidence interval width instead of a hard min_n flag?
  • What if metric contains NaNs?
  • Many segments fail the threshold. How would you still report something useful for them without discarding their data?