Reshape a wide pandas DataFrame to long format with stack, dropping NaN cells

Quick Overview

Reshape a wide pandas DataFrame with a student column and one numeric column per subject into one row per student and subject score, using set_index and stack. Tests index level naming, reset_index, row ordering and how missing cells are dropped across pandas versions.

Reshape a wide pandas DataFrame to long format with stack, dropping NaN cells

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `stack_to_long(df)` to reshape a wide DataFrame (a `'student'` column plus one numeric column per subject) into one row per (student, subject) score. Use `df.set_index('student').stack()`, name the inner level `'subject'` and the value `'score'`, then `reset_index()`. The result must meet these requirements: - Columns exactly `['student', 'subject', 'score']` with a fresh `RangeIndex`. - NaN cells produce no row. - Rows are grouped by student in the original row order, and within each student the subjects follow the original column order. Example: ```python df = pd.DataFrame({ 'student': ['ann', 'bob'], 'math': [90, 70], 'science': [85, 60], }) stack_to_long(df) # student subject score # 0 ann math 90 # 1 ann science 85 # 2 bob math 70 # 3 bob science 60 ``` ```hint Missing cells Check whether the `stack` you are calling drops NaN cells by itself. That default is not the same in every pandas version. ``` ### Constraints & Assumptions - The input has a `'student'` column and one numeric column per subject, and no other columns. - Any subject cell may be NaN. ### What a Strong Answer Covers - Moving `student` into the index so only the subject columns are stacked, and naming the new level `subject` and the values `score`. - Dropping NaN cells in a way that does not depend on the default behavior of `stack` in a given pandas version. - Returning exactly the three required columns with a fresh `RangeIndex`. - Why the rows follow the original row and column order without a sort. ### Follow-up Questions - How would you keep NaN cells as explicit rows instead of dropping them? - How does `melt` compare to `stack` here? - What happens to the dtype of `score` when a subject column contains NaN?

Overview: Reshape a wide pandas DataFrame with a student column and one numeric column per subject into one row per student and subject score, using set_index and stack. Tests index level naming, reset_index, row ordering and how missing cells are dropped across pandas versions.

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

Write stack_to_long(df) to reshape a wide DataFrame (a 'student' column plus one numeric column per subject) into one row per (student, subject) score. Use df.set_index('student').stack(), name the inner level 'subject' and the value 'score', then reset_index().

The result must meet these requirements:

  • Columns exactly ['student', 'subject', 'score'] with a fresh RangeIndex .
  • NaN cells produce no row.
  • Rows are grouped by student in the original row order, and within each student the subjects follow the original column order.

Example:

df = pd.DataFrame({
    'student': ['ann', 'bob'],
    'math':    [90, 70],
    'science': [85, 60],
})

stack_to_long(df)
#   student  subject  score
# 0     ann     math     90
# 1     ann  science     85
# 2     bob     math     70
# 3     bob  science     60

Constraints & Assumptions

  • The input has a 'student' column and one numeric column per subject, and no other columns.
  • Any subject cell may be NaN.

What a Strong Answer Covers Guidance

  • Moving student into the index so only the subject columns are stacked, and naming the new level subject and the values score .
  • Dropping NaN cells in a way that does not depend on the default behavior of stack in a given pandas version.
  • Returning exactly the three required columns with a fresh RangeIndex .
  • Why the rows follow the original row and column order without a sort.

Follow-up Questions Guidance

  • How would you keep NaN cells as explicit rows instead of dropping them?
  • How does melt compare to stack here?
  • What happens to the dtype of score when a subject column contains NaN?