Filter a pandas DataFrame with DataFrame.query and @ variables

Quick Overview

Practice filtering a pandas DataFrame with DataFrame.query, using the @ prefix to reference function arguments for a department match and an inclusive salary bound. It tests query string syntax, variable scoping, and keeping the original index and row order.

Filter a pandas DataFrame with DataFrame.query and @ variables

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `query_min_salary(df, dept, min_salary)` that filters an employee table with `DataFrame.query`. Return the rows where `department` equals `dept` and `salary` is at least `min_salary`. Requirements: - Use `DataFrame.query` for the filter, referencing the function arguments with `@`. Do not build a boolean mask and index with it. - Match `department == @dept` and `salary >= @min_salary` (the salary bound is inclusive). - Keep all columns and preserve the original index and row order. - Return a `DataFrame` (a filtered copy of the input). For example: ```python df = pd.DataFrame({ 'name': ['A', 'B', 'C', 'D'], 'department': ['eng', 'sales', 'eng', 'eng'], 'salary': [90000, 70000, 120000, 60000], }) query_min_salary(df, 'eng', 90000) # name department salary # 0 A eng 90000 # 2 C eng 120000 ``` ```hint Building the expression Both conditions fit in one query string. A bare name in that string is read as a column, so think about how the parser learns that a name refers to a function argument, and check which comparison operator makes the salary bound inclusive. ``` ### Constraints & Assumptions - The columns `department` and `salary` always exist. ### What a Strong Answer Covers - A single query expression that combines the department match and the inclusive salary bound. - Correct use of `@` for the function arguments, and what happens when the prefix is left out. - Confirmation that the result keeps every column, the original index labels and the row order. - Behavior on edge cases such as no matching rows or missing salary values. ### Follow-up Questions - How would you filter on a column whose name contains a space or special character inside `query`? - When is `query` faster than a boolean mask, and when is it not? - Why is `@dept` safer than formatting the department value into the query string with an f-string?

Overview: Practice filtering a pandas DataFrame with DataFrame.query, using the @ prefix to reference function arguments for a department match and an inclusive salary bound. It tests query string syntax, variable scoping, and keeping the original index and row order.

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

Implement query_min_salary(df, dept, min_salary) that filters an employee table with DataFrame.query. Return the rows where department equals dept and salary is at least min_salary.

Requirements:

  • Use DataFrame.query for the filter, referencing the function arguments with @ . Do not build a boolean mask and index with it.
  • Match department == @dept and salary >= @min_salary (the salary bound is inclusive).
  • Keep all columns and preserve the original index and row order.
  • Return a DataFrame (a filtered copy of the input).

For example:

df = pd.DataFrame({
    'name': ['A', 'B', 'C', 'D'],
    'department': ['eng', 'sales', 'eng', 'eng'],
    'salary': [90000, 70000, 120000, 60000],
})

query_min_salary(df, 'eng', 90000)
#    name department  salary
# 0     A        eng   90000
# 2     C        eng  120000

Constraints & Assumptions

  • The columns department and salary always exist.

What a Strong Answer Covers Guidance

  • A single query expression that combines the department match and the inclusive salary bound.
  • Correct use of @ for the function arguments, and what happens when the prefix is left out.
  • Confirmation that the result keeps every column, the original index labels and the row order.
  • Behavior on edge cases such as no matching rows or missing salary values.

Follow-up Questions Guidance

  • How would you filter on a column whose name contains a space or special character inside query ?
  • When is query faster than a boolean mask, and when is it not?
  • Why is @dept safer than formatting the department value into the query string with an f-string?