Filter DataFrame Rows That Pass Multiple Validation Rules in Pandas

Quick Overview

Implement a pandas function that keeps only the rows of a user table that pass an age range check, an email check and a country allowlist, returning a new frame with a reset index. It tests boolean masks, literal string matching, missing-value handling and order-preserving filtering.

Filter DataFrame Rows That Pass Multiple Validation Rules in Pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `clean_records(df: pd.DataFrame) -> pd.DataFrame`. `df` has columns `['user_id', 'age', 'email', 'country']` with types int, int, str, str. Return a new frame with only the rows that pass all rules, without mutating the input. The rules: - `age` is within the inclusive range `[18, 99]`. - `email` contains a literal `'@'`. - `country` is in `{'US', 'UK', 'CA'}`. The output must preserve the column order and the relative order of the surviving rows, with the index reset to a default `RangeIndex`. If all rows are dropped, return an empty frame with the same columns. For example: ```python df = pd.DataFrame({ "user_id": [1, 2, 3, 4, 5], "age": [25, 17, 40, 55, 30], "email": ["a@x.com", "b@x.com", "no-at", "c@x.com", "d@x.com"], "country": ["US", "UK", "CA", "FR", "CA"], }) # 1, 25, 'a@x.com', 'US' -> valid # 2, 17, 'b@x.com', 'UK' -> age < 18, drop # 3, 40, 'no-at', 'CA' -> no '@', drop # 4, 55, 'c@x.com', 'FR' -> country not allowed, drop # 5, 30, 'd@x.com', 'CA' -> valid clean_records(df) # user_id age email country # 0 1 25 a@x.com US # 1 5 30 d@x.com CA ``` The output keeps rows 1 and 5, with the index reset to `[0, 1]`. ```hint One mask per rule Express each rule as its own boolean Series over the rows. Then decide how to combine them, and what the index looks like after you select. ``` ```hint Check the string match Look at how `str.contains` treats its pattern by default, and what it returns for a missing value. ``` ### What a Strong Answer Covers - Vectorized checks for each rule, combined so that a row survives only when every rule passes. - A literal match for the `@` check, and what happens to a missing email. - Row order, column order, the reset index and the all-dropped case. - Leaving the input frame unchanged. ### Follow-up Questions - How would you return the dropped rows and the reason each failed? - How would you make the rules configurable instead of hard-coded? - How would the rules behave if `age` could be missing or `country` arrived with stray spaces or lowercase codes?

Overview: Implement a pandas function that keeps only the rows of a user table that pass an age range check, an email check and a country allowlist, returning a new frame with a reset index. It tests boolean masks, literal string matching, missing-value handling and order-preserving filtering.

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

Implement clean_records(df: pd.DataFrame) -> pd.DataFrame. df has columns ['user_id', 'age', 'email', 'country'] with types int, int, str, str. Return a new frame with only the rows that pass all rules, without mutating the input.

The rules:

  • age is within the inclusive range [18, 99] .
  • email contains a literal '@' .
  • country is in {'US', 'UK', 'CA'} .

The output must preserve the column order and the relative order of the surviving rows, with the index reset to a default RangeIndex. If all rows are dropped, return an empty frame with the same columns.

For example:

df = pd.DataFrame({
    "user_id": [1, 2, 3, 4, 5],
    "age": [25, 17, 40, 55, 30],
    "email": ["a@x.com", "b@x.com", "no-at", "c@x.com", "d@x.com"],
    "country": ["US", "UK", "CA", "FR", "CA"],
})
# 1, 25, 'a@x.com', 'US'   -> valid
# 2, 17, 'b@x.com', 'UK'   -> age < 18, drop
# 3, 40, 'no-at',   'CA'   -> no '@', drop
# 4, 55, 'c@x.com', 'FR'   -> country not allowed, drop
# 5, 30, 'd@x.com', 'CA'   -> valid

clean_records(df)
#    user_id  age    email  country
# 0        1   25  a@x.com       US
# 1        5   30  d@x.com       CA

The output keeps rows 1 and 5, with the index reset to [0, 1].

What a Strong Answer Covers Guidance

  • Vectorized checks for each rule, combined so that a row survives only when every rule passes.
  • A literal match for the @ check, and what happens to a missing email.
  • Row order, column order, the reset index and the all-dropped case.
  • Leaving the input frame unchanged.

Follow-up Questions Guidance

  • How would you return the dropped rows and the reason each failed?
  • How would you make the rules configurable instead of hard-coded?
  • How would the rules behave if age could be missing or country arrived with stray spaces or lowercase codes?