Filter out rows whose column value is in a list (SQL NOT IN) in pandas
Quick Overview
Write a pandas function that drops rows whose column value appears in a given list, the equivalent of SQL NOT IN, while keeping every column and the original index. It tests boolean masks, element-wise negation, and how missing values behave compared with SQL.
Filter out rows whose column value is in a list (SQL NOT IN) in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `exclude_values(df, col, values)`, the pandas equivalent of SQL `NOT IN`. Return rows whose `col` value is not in `values`, by negating an `.isin` mask with `~`.
The function must meet these requirements:
- Keep every row whose `col` value is not in `values`.
- Retain all columns without dropping or reordering them.
- Preserve the original index and row order.
- An empty `values` list returns all rows.
Example:
```python
df = pd.DataFrame({
'id': [1, 2, 3, 4],
'status': ['open', 'closed', 'open', 'void'],
})
exclude_values(df, 'status', ['closed', 'void'])
# id status
# 0 1 open
# 2 3 open
```
```hint Missing values
Before you finish, work out what your mask does for a row where `col` is `NaN`, and compare that with how SQL `NOT IN` treats a NULL.
```
### What a Strong Answer Covers
- Building a boolean membership mask and inverting it element by element
- Why boolean indexing keeps all columns, the original index and the row order
- The empty `values` case
- How missing values behave, and where that differs from SQL `NOT IN`
### Follow-up Questions
- How would you handle `NaN` values in the column?
- How would you exclude on multiple columns at once?
- How would you exclude rows by a combination of two columns, the way SQL `(a, b) NOT IN (...)` works?
Overview: Write a pandas function that drops rows whose column value appears in a given list, the equivalent of SQL NOT IN, while keeping every column and the original index. It tests boolean masks, element-wise negation, and how missing values behave compared with SQL.
Implement exclude_values(df, col, values), the pandas equivalent of SQL NOT IN. Return rows whose col value is not in values, by negating an .isin mask with ~.
The function must meet these requirements:
Keep every row whose
col
value is not in
values
.
Retain all columns without dropping or reordering them.
Preserve the original index and row order.
An empty
values
list returns all rows.
Example:
df = pd.DataFrame({
'id': [1, 2, 3, 4],
'status': ['open', 'closed', 'open', 'void'],
})
exclude_values(df, 'status', ['closed', 'void'])
# id status
# 0 1 open
# 2 3 open
What a Strong Answer Covers Guidance
Building a boolean membership mask and inverting it element by element
Why boolean indexing keeps all columns, the original index and the row order
The empty
values
case
How missing values behave, and where that differs from SQL
NOT IN
Follow-up Questions Guidance
How would you handle
NaN
values in the column?
How would you exclude on multiple columns at once?
How would you exclude rows by a combination of two columns, the way SQL
(a, b) NOT IN (...)
works?