Filter a pandas DataFrame to rows with prices in a valid range

Quick Overview

Write a pandas function that drops rows whose price falls outside an inclusive 0.0 to 1000.0 range, keeps the original column and row order, and returns a fresh RangeIndex. It tests boolean masking, inclusive bounds, how NaN behaves in comparisons, and index resetting.

Filter a pandas DataFrame to rows with prices in a valid range

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `drop_out_of_range(df)`. The input DataFrame has columns `['product', 'price']`, where `price` is a float column that includes some invalid prices. Return the filtered DataFrame itself (not a boolean mask), keeping only the rows with a valid price. ```python import pandas as pd def drop_out_of_range(df: pd.DataFrame) -> pd.DataFrame: ... ``` For example: ```text Input: product price 0 apple 2.50 1 gold -5.00 2 banana 99999.00 3 bread 3.20 Output: product price 0 apple 2.50 1 bread 3.20 ``` `gold` is below the lower bound and `banana` is above the upper bound, so only `apple` and `bread` remain, renumbered 0 and 1. ```hint Building the filter A boolean Series over `price` selects the rows in one step. Check whether the method or operators you pick include both bounds. ``` ### Constraints & Assumptions - A price is valid when `0.0 <= price <= 1000.0`. Both bounds are inclusive. - The result keeps the column order `['product', 'price']` and the relative order of the surviving rows. - The result has a fresh default `RangeIndex` running from `0` to `k-1`, where `k` is the number of rows kept. ### What a Strong Answer Covers - An inclusive range test on `price` that keeps rows priced exactly 0.0 or 1000.0. - Returning a DataFrame with the original columns and row order intact. - Renumbering the index without leaving the old labels behind as a new column. - What the filter does with `NaN` and infinite prices. ### Follow-up Questions - How would you also handle `NaN` prices? - Why call `reset_index(drop=True)` instead of returning the filtered slice as-is? - What would change if `price` arrived as a column of strings, some with a currency symbol or a thousands separator?

Overview: Write a pandas function that drops rows whose price falls outside an inclusive 0.0 to 1000.0 range, keeps the original column and row order, and returns a fresh RangeIndex. It tests boolean masking, inclusive bounds, how NaN behaves in comparisons, and index resetting.

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

Implement drop_out_of_range(df). The input DataFrame has columns ['product', 'price'], where price is a float column that includes some invalid prices. Return the filtered DataFrame itself (not a boolean mask), keeping only the rows with a valid price.

import pandas as pd


def drop_out_of_range(df: pd.DataFrame) -> pd.DataFrame:
    ...

For example:

Input:
   product     price
0   apple       2.50
1   gold       -5.00
2   banana  99999.00
3   bread       3.20

Output:
   product  price
0   apple    2.50
1   bread    3.20

gold is below the lower bound and banana is above the upper bound, so only apple and bread remain, renumbered 0 and 1.

Constraints & Assumptions

  • A price is valid when 0.0 <= price <= 1000.0 . Both bounds are inclusive.
  • The result keeps the column order ['product', 'price'] and the relative order of the surviving rows.
  • The result has a fresh default RangeIndex running from 0 to k-1 , where k is the number of rows kept.

What a Strong Answer Covers Guidance

  • An inclusive range test on price that keeps rows priced exactly 0.0 or 1000.0.
  • Returning a DataFrame with the original columns and row order intact.
  • Renumbering the index without leaving the old labels behind as a new column.
  • What the filter does with NaN and infinite prices.

Follow-up Questions Guidance

  • How would you also handle NaN prices?
  • Why call reset_index(drop=True) instead of returning the filtered slice as-is?
  • What would change if price arrived as a column of strings, some with a currency symbol or a thousands separator?