Promote the First Row of a pandas DataFrame to Column Headers

Quick Overview

Practice fixing a pandas DataFrame whose real header sits in the first data row. The task tests how you build column labels from a row, slice off the body, clear leftover axis names, reset the index and leave the input frame unchanged.

Promote the First Row of a pandas DataFrame to Column Headers

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def promote_header(df: pd.DataFrame) -> pd.DataFrame`. The input has integer column labels (e.g. `[0, 1, 2]`), the real header strings in the first row, and records in the following rows (all object dtype). Promote the first row to column labels and return the remaining records. Requirements: - Columns become the first row's values, with columns axis name `None`. - Drop the first row from the body and keep remaining records in original order. - Reset to a default `RangeIndex` `0..k-1` (old index not kept as a column). - Return a new `DataFrame` without mutating the input. Example: ```python # input (columns are 0,1,2) # 0 1 2 # 0 name age city # 1 Ada 36 Paris # 2 Bo 29 Rome promote_header(df) # name age city # 0 Ada 36 Paris # 1 Bo 29 Rome ``` ```hint Leftover labels After you assign the new labels, inspect every attribute of the result. The columns axis and the row index can both carry traces of the frame they came from. ``` ### What a Strong Answer Covers - Taking the labels from the first row and the body from the remaining rows in their original order. - The name that the columns axis picks up from the first row, and how to clear it. - A fresh `RangeIndex` with the old index dropped, not added back as a column. - Leaving the input untouched, and what happens on an empty frame or a header-only frame. ### Follow-up Questions - What if header cells contain duplicates or surrounding whitespace? - How would you generalize to a header that spans the first N rows? - The body columns are still object dtype after the promotion. How would you recover numeric types?

Overview: Practice fixing a pandas DataFrame whose real header sits in the first data row. The task tests how you build column labels from a row, slice off the body, clear leftover axis names, reset the index and leave the input frame unchanged.

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

Implement def promote_header(df: pd.DataFrame) -> pd.DataFrame. The input has integer column labels (e.g. [0, 1, 2]), the real header strings in the first row, and records in the following rows (all object dtype). Promote the first row to column labels and return the remaining records.

Requirements:

  • Columns become the first row's values, with columns axis name None .
  • Drop the first row from the body and keep remaining records in original order.
  • Reset to a default RangeIndex 0..k-1 (old index not kept as a column).
  • Return a new DataFrame without mutating the input.

Example:

# input (columns are 0,1,2)
#      0     1      2
# 0  name   age   city
# 1  Ada    36    Paris
# 2  Bo     29    Rome

promote_header(df)
#    name  age   city
# 0  Ada   36   Paris
# 1  Bo    29   Rome

What a Strong Answer Covers Guidance

  • Taking the labels from the first row and the body from the remaining rows in their original order.
  • The name that the columns axis picks up from the first row, and how to clear it.
  • A fresh RangeIndex with the old index dropped, not added back as a column.
  • Leaving the input untouched, and what happens on an empty frame or a header-only frame.

Follow-up Questions Guidance

  • What if header cells contain duplicates or surrounding whitespace?
  • How would you generalize to a header that spans the first N rows?
  • The body columns are still object dtype after the promotion. How would you recover numeric types?