Keep rows from pandas groups whose sum exceeds a threshold

Quick Overview

Write a pandas function that groups rows by a key column and returns the original rows of every group whose total of a value column is strictly above a threshold. It tests group-level filtering, strict comparisons, index and order preservation, and missing keys.

Keep rows from pandas groups whose sum exceeds a threshold

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `keep_large_groups(df, key, val, threshold)`. Group rows by `key` and return only the rows of groups whose sum of `val` is strictly greater than `threshold`. Requirements: - The comparison is strict (`>`): a group summing exactly to `threshold` is excluded. - Return full rows with all original columns, not grouped sums. - Preserve the original row order and index of the surviving rows. Example: ```python df = pd.DataFrame({ 'key': ['a', 'b', 'a', 'b', 'c'], 'val': [10, 1, 5, 2, 100], }) keep_large_groups(df, 'key', 'val', threshold=10) # key val # 0 a 10 # group 'a' sum = 15 > 10 # 2 a 5 # 4 c 100 # group 'c' sum = 100 > 10 # group 'b' sum = 3, dropped ``` ```hint Group decision, row output The keep-or-drop decision is made once per group, but the output is made of rows. Think about how a group-level number gets back to the individual rows, and keep the comparison strict. ``` ### Constraints & Assumptions - `key` names one column of any hashable type and `val` names a numeric column. - `threshold` is a number. The frame may have any number of other columns, which must come through unchanged. ### What a Strong Answer Covers - Returning the full original rows of the passing groups with every column - The strict comparison, so a group whose sum equals the threshold is dropped - Keeping the original index and row order of the survivors - The cost of a per-group Python predicate compared with a vectorized boolean mask - What happens to rows with a missing key or missing values ### Follow-up Questions - How does `groupby().filter()` differ from `transform()` for this task? - How would you keep only the groups whose sum is above the q-th quantile of all group sums? - Rows whose `key` is missing disappear from the result. Why, and how would you keep them as their own group?

Overview: Write a pandas function that groups rows by a key column and returns the original rows of every group whose total of a value column is strictly above a threshold. It tests group-level filtering, strict comparisons, index and order preservation, and missing keys.

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

Implement keep_large_groups(df, key, val, threshold). Group rows by key and return only the rows of groups whose sum of val is strictly greater than threshold.

Requirements:

  • The comparison is strict ( > ): a group summing exactly to threshold is excluded.
  • Return full rows with all original columns, not grouped sums.
  • Preserve the original row order and index of the surviving rows.

Example:

df = pd.DataFrame({
    'key': ['a', 'b', 'a', 'b', 'c'],
    'val': [10, 1, 5, 2, 100],
})
keep_large_groups(df, 'key', 'val', threshold=10)
#   key  val
# 0   a   10   # group 'a' sum = 15 > 10
# 2   a    5
# 4   c  100   # group 'c' sum = 100 > 10
# group 'b' sum = 3, dropped

Constraints & Assumptions

  • key names one column of any hashable type and val names a numeric column.
  • threshold is a number. The frame may have any number of other columns, which must come through unchanged.

What a Strong Answer Covers Guidance

  • Returning the full original rows of the passing groups with every column
  • The strict comparison, so a group whose sum equals the threshold is dropped
  • Keeping the original index and row order of the survivors
  • The cost of a per-group Python predicate compared with a vectorized boolean mask
  • What happens to rows with a missing key or missing values

Follow-up Questions Guidance

  • How does groupby().filter() differ from transform() for this task?
  • How would you keep only the groups whose sum is above the q-th quantile of all group sums?
  • Rows whose key is missing disappear from the result. Why, and how would you keep them as their own group?