Filter DataFrame Rows Where a Column Contains a Substring, Ignoring Case

Quick Overview

Write a pandas function that returns the DataFrame rows whose text column contains a given substring in any letter case. It tests vectorized string matching, missing-value handling in boolean masks, how regex metacharacters affect a search term, and keeping the original index and row order.

Filter DataFrame Rows Where a Column Contains a Substring, Ignoring Case

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `filter_contains(df, col, sub)` that returns the rows of `df` whose string column `col` contains the substring `sub`, case-insensitively. The function must meet these requirements: - Return all columns of the matching rows. - Preserve the original index and row order. - Exclude rows where `col` is `NaN` or `None`, without raising an error. Example: ```python import pandas as pd df = pd.DataFrame({"name": ["Alice", "bob", None, "CAROL"]}) filter_contains(df, "name", "o") # name # 1 bob # 3 CAROL ``` The original index labels 1 and 3 are kept. ```hint Pitfalls Check what your string test does with a missing value, and with a `sub` such as `"a.b"` or `"c++"`. ``` ### What a Strong Answer Covers - A vectorized, case-insensitive substring test on `col`. - Handling of missing values so the mask is purely boolean and the selection never raises. - Whether `sub` is read as plain text or as a pattern, and why that matters for arbitrary input. - A selection that returns every column and keeps the original index labels and order. - Behavior for edge inputs such as an empty `sub` or a column that is not a string dtype. ### Follow-up Questions - What goes wrong if `sub` contains regex metacharacters such as `.` or `(` and the method reads it as a regular expression? - What if `col` is not actually a string dtype? - How would you keep rows that match any one of several substrings, or that match in any of several columns?

Overview: Write a pandas function that returns the DataFrame rows whose text column contains a given substring in any letter case. It tests vectorized string matching, missing-value handling in boolean masks, how regex metacharacters affect a search term, and keeping the original index and row order.

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

Implement filter_contains(df, col, sub) that returns the rows of df whose string column col contains the substring sub, case-insensitively.

The function must meet these requirements:

  • Return all columns of the matching rows.
  • Preserve the original index and row order.
  • Exclude rows where col is NaN or None , without raising an error.

Example:

import pandas as pd

df = pd.DataFrame({"name": ["Alice", "bob", None, "CAROL"]})
filter_contains(df, "name", "o")
#     name
# 1    bob
# 3  CAROL

The original index labels 1 and 3 are kept.

What a Strong Answer Covers Guidance

  • A vectorized, case-insensitive substring test on col .
  • Handling of missing values so the mask is purely boolean and the selection never raises.
  • Whether sub is read as plain text or as a pattern, and why that matters for arbitrary input.
  • A selection that returns every column and keeps the original index labels and order.
  • Behavior for edge inputs such as an empty sub or a column that is not a string dtype.

Follow-up Questions Guidance

  • What goes wrong if sub contains regex metacharacters such as . or ( and the method reads it as a regular expression?
  • What if col is not actually a string dtype?
  • How would you keep rows that match any one of several substrings, or that match in any of several columns?