Reshape wide student scores to long format with pandas stack

Quick Overview

Reshape a wide pandas DataFrame of student scores into long format with stack, returning one row per student and subject with exact column names, a fresh index and a fixed row order. Tests how stack builds a MultiIndex, how reset_index names the new columns, and how stack ordering compares with melt.

Reshape wide student scores to long format with pandas stack

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `wide_scores_to_long(df)`. The input is a wide DataFrame with one row per student and one column per subject. Use `stack` to reshape it into one row per `(student, subject, score)`. The result must meet these requirements: - Return columns exactly `['student', 'subject', 'score']`. - Attach a fresh default `RangeIndex`. - Order: all of a student's subjects before the next student, subjects in original column order. Example input (wide): ```text math science history student ann 90 85 70 bob 60 75 80 ``` Expected output (long): ```text student subject score 0 ann math 90 1 ann science 85 2 ann history 70 3 bob math 60 4 bob science 75 5 bob history 80 ``` ```hint What stack gives you Look at what `df.stack()` returns: its type, its index levels, and where each level gets its name. ``` ### Constraints & Assumptions - The row index is named `'student'`. - The columns are the subjects `['math', 'science', 'history']`, in that order. - Every score is an integer. ### What a Strong Answer Covers - How the subject columns become an index level, and how that level and the value column get the names `subject` and `score`. - Turning the index levels into columns with a fresh default `RangeIndex` and the exact column order. - Why the rows come out student by student with subjects in the original column order, and whether any sort is needed. - The cost in terms of the number of students and subjects, and what would happen to a missing score. ### Follow-up Questions - How does `stack` ordering differ from `melt` here, and what would it take to get the same layout from `melt`? - Why call `reset_index`, and how do you name the new columns? - What happens to a student whose score in one subject is missing?

Overview: Reshape a wide pandas DataFrame of student scores into long format with stack, returning one row per student and subject with exact column names, a fresh index and a fixed row order. Tests how stack builds a MultiIndex, how reset_index names the new columns, and how stack ordering compares with melt.

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

Implement wide_scores_to_long(df). The input is a wide DataFrame with one row per student and one column per subject. Use stack to reshape it into one row per (student, subject, score).

The result must meet these requirements:

  • Return columns exactly ['student', 'subject', 'score'] .
  • Attach a fresh default RangeIndex .
  • Order: all of a student's subjects before the next student, subjects in original column order.

Example input (wide):

         math  science  history
student
ann        90       85       70
bob        60       75       80

Expected output (long):

  student  subject  score
0     ann     math     90
1     ann  science     85
2     ann  history     70
3     bob     math     60
4     bob  science     75
5     bob  history     80

Constraints & Assumptions

  • The row index is named 'student' .
  • The columns are the subjects ['math', 'science', 'history'] , in that order.
  • Every score is an integer.

What a Strong Answer Covers Guidance

  • How the subject columns become an index level, and how that level and the value column get the names subject and score .
  • Turning the index levels into columns with a fresh default RangeIndex and the exact column order.
  • Why the rows come out student by student with subjects in the original column order, and whether any sort is needed.
  • The cost in terms of the number of students and subjects, and what would happen to a missing score.

Follow-up Questions Guidance

  • How does stack ordering differ from melt here, and what would it take to get the same layout from melt ?
  • Why call reset_index , and how do you name the new columns?
  • What happens to a student whose score in one subject is missing?