Normalize messy DataFrame column labels to snake_case

Quick Overview

Write a pandas function that renames messy DataFrame column headers to snake_case using a fixed lowercase, regex and strip rule, without mutating the input. It tests regex handling, pandas copy semantics and edge cases such as labels that collide or become empty after normalization.

Normalize messy DataFrame column labels to snake_case

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `snake_case_columns(df: pd.DataFrame) -> pd.DataFrame`. The column headers are messy (mixed case, punctuation, currency symbols, irregular spacing). Return a new DataFrame with canonical snake_case column labels. Requirements: - Per label, in order: lowercase, strip outer whitespace, apply `re.sub(r'[^0-9a-z]+', '_', label)`, then strip leading and trailing underscores. - Do not mutate the input. Only the column labels change. - Row values and the index must be preserved exactly. Example: ```python cols = ['Customer ID ', 'Total $ Spent', 'E-mail', 'Sign-Up Date'] # -> ['customer_id', 'total_spent', 'e_mail', 'sign_up_date'] ``` ```hint Watch the caller's frame Assigning to `df.columns` renames the frame you were handed. Work on an object the caller doesn't own. ``` ### What a Strong Answer Covers - Applies the four steps in the stated order and explains why the order matters. - Returns a new DataFrame and leaves the input's labels, values and index unchanged. - Handles column labels that are not strings. - Traces the example and names the inputs the rule leaves open, such as collisions and labels that end up empty. ### Follow-up Questions - How would you handle labels that collide after normalization (for example `'E-mail'` and `'e mail'`)? - What if a label normalizes to an empty string (for example `'$$$'`)? - How does the rule treat accented labels such as `'Café'`, and what would you change if those names should survive?

Overview: Write a pandas function that renames messy DataFrame column headers to snake_case using a fixed lowercase, regex and strip rule, without mutating the input. It tests regex handling, pandas copy semantics and edge cases such as labels that collide or become empty after normalization.

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

Implement snake_case_columns(df: pd.DataFrame) -> pd.DataFrame. The column headers are messy (mixed case, punctuation, currency symbols, irregular spacing). Return a new DataFrame with canonical snake_case column labels.

Requirements:

  • Per label, in order: lowercase, strip outer whitespace, apply re.sub(r'[^0-9a-z]+', '_', label) , then strip leading and trailing underscores.
  • Do not mutate the input. Only the column labels change.
  • Row values and the index must be preserved exactly.

Example:

cols = ['Customer ID ', 'Total $ Spent', 'E-mail', 'Sign-Up Date']
# -> ['customer_id', 'total_spent', 'e_mail', 'sign_up_date']

What a Strong Answer Covers Guidance

  • Applies the four steps in the stated order and explains why the order matters.
  • Returns a new DataFrame and leaves the input's labels, values and index unchanged.
  • Handles column labels that are not strings.
  • Traces the example and names the inputs the rule leaves open, such as collisions and labels that end up empty.

Follow-up Questions Guidance

  • How would you handle labels that collide after normalization (for example 'E-mail' and 'e mail' )?
  • What if a label normalizes to an empty string (for example '$$$' )?
  • How does the rule treat accented labels such as 'Café' , and what would you change if those names should survive?