Select pandas DataFrame columns whose names match a regex

Quick Overview

Write a pandas function that keeps only the DataFrame columns whose names match a regular expression while leaving every row, the index and the column order unchanged. It tests DataFrame.filter, regex search semantics and anchors, and edge cases such as no matches or special characters in column names.

Select pandas DataFrame columns whose names match a regex

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `select_matching_cols(df: pd.DataFrame, pattern: str) -> pd.DataFrame` that returns only the columns whose name matches the regex `pattern`, using `DataFrame.filter(regex=pattern, axis=1)`. Match column labels only, not the index or data. Requirements: - Regex search, not full-string match. - Preserve the original column order. - Keep every row and the original index intact. - Return a new DataFrame: do not mutate `df`. Example: ```python list(df.columns) # ['id', 'sales_2022', 'sales_2023', 'cost_2023'] list(select_matching_cols(df, '_2023$').columns) # ['sales_2023', 'cost_2023'] ``` ```hint Search semantics With search semantics an unanchored pattern can match anywhere inside a label. Think about which labels `'_2023'` would select without the `$`, and what a literal `.` in a pattern matches. ``` ### What a Strong Answer Covers - A call that matches the pattern against column labels only and leaves values and the index alone. - How the pattern is applied to each label, including anchors and labels that are not strings. - Why column order, every row and the index survive, and why `df` itself is unchanged. - Edge cases such as a pattern that matches nothing, an invalid pattern, or regex metacharacters in a literal column name. ### Follow-up Questions - How would you filter columns by a case-insensitive pattern? - How does `filter(regex=...)` differ from `filter(like=...)`? - How would you require the whole column name to match, so that `'sales'` does not select `'sales_2023'`?

Overview: Write a pandas function that keeps only the DataFrame columns whose names match a regular expression while leaving every row, the index and the column order unchanged. It tests DataFrame.filter, regex search semantics and anchors, and edge cases such as no matches or special characters in column names.

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

Implement select_matching_cols(df: pd.DataFrame, pattern: str) -> pd.DataFrame that returns only the columns whose name matches the regex pattern, using DataFrame.filter(regex=pattern, axis=1). Match column labels only, not the index or data.

Requirements:

  • Regex search, not full-string match.
  • Preserve the original column order.
  • Keep every row and the original index intact.
  • Return a new DataFrame: do not mutate df .

Example:

list(df.columns)
# ['id', 'sales_2022', 'sales_2023', 'cost_2023']

list(select_matching_cols(df, '_2023$').columns)
# ['sales_2023', 'cost_2023']

What a Strong Answer Covers Guidance

  • A call that matches the pattern against column labels only and leaves values and the index alone.
  • How the pattern is applied to each label, including anchors and labels that are not strings.
  • Why column order, every row and the index survive, and why df itself is unchanged.
  • Edge cases such as a pattern that matches nothing, an invalid pattern, or regex metacharacters in a literal column name.

Follow-up Questions Guidance

  • How would you filter columns by a case-insensitive pattern?
  • How does filter(regex=...) differ from filter(like=...) ?
  • How would you require the whole column name to match, so that 'sales' does not select 'sales_2023' ?