Filter pandas DataFrame rows where a numeric column lies in a closed range
Quick Overview
Write a pandas function that returns the DataFrame rows whose numeric column falls inside a closed range, including both bounds. It tests Series.between with the inclusive option, keeping the index and row order without mutating the input, and handling missing values and edge bounds.
Filter pandas DataFrame rows where a numeric column lies in a closed range
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `filter_between(df, col, lo, hi)` that returns the rows whose numeric column `col` lies in the closed interval [`lo`, `hi`]. Use `Series.between` with `inclusive='both'`.
Requirements:
- Include both bounds.
- Keep all columns of `df` unchanged.
- Preserve the original index and row order.
- Return a new filtered `DataFrame` and do not mutate the input.
Assume `col` exists and is numeric.
Example:
```python
df = pd.DataFrame({'item': ['a', 'b', 'c', 'd'], 'price': [5, 10, 15, 25]})
filter_between(df, 'price', 10, 20)
# item price
# 1 b 10
# 2 c 15
```
```hint Test the boundaries
Check your mask on a value exactly equal to `lo`, one exactly equal to `hi`, and a missing value.
```
### What a Strong Answer Covers
- Builds the mask with `Series.between` and `inclusive='both'` so both endpoints qualify.
- Selects rows with the mask so every column, the index labels and the row order are kept.
- Shows that the input frame is left unmodified.
- Explains what happens to missing values in `col` and to a range where `lo` is greater than `hi`.
- States the time and space cost of the filter.
### Follow-up Questions
- How would you make the interval exclusive on one side, or on both sides?
- The column holds timestamps and the caller passes two calendar dates. How do you make sure every row on the last date is included?
- Each row has a category, and each category has its own allowed price range stored in a dictionary. How would you filter every row against its own range without a loop?
Overview: Write a pandas function that returns the DataFrame rows whose numeric column falls inside a closed range, including both bounds. It tests Series.between with the inclusive option, keeping the index and row order without mutating the input, and handling missing values and edge bounds.
Implement filter_between(df, col, lo, hi) that returns the rows whose numeric column col lies in the closed interval [lo, hi]. Use Series.between with inclusive='both'.
Requirements:
Include both bounds.
Keep all columns of
df
unchanged.
Preserve the original index and row order.
Return a new filtered
DataFrame
and do not mutate the input.
Builds the mask with
Series.between
and
inclusive='both'
so both endpoints qualify.
Selects rows with the mask so every column, the index labels and the row order are kept.
Shows that the input frame is left unmodified.
Explains what happens to missing values in
col
and to a range where
lo
is greater than
hi
.
States the time and space cost of the filter.
Follow-up Questions Guidance
How would you make the interval exclusive on one side, or on both sides?
The column holds timestamps and the caller passes two calendar dates. How do you make sure every row on the last date is included?
Each row has a category, and each category has its own allowed price range stored in a dictionary. How would you filter every row against its own range without a loop?