Melt a Wide Survey DataFrame to Long Form in pandas

Quick Overview

Reshape a wide survey DataFrame with one column per question into long form, with one row per respondent and question. It tests pd.melt arguments for identifier and value columns, naming the new columns, and the row order and index that melt produces.

Melt a Wide Survey DataFrame to Long Form in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `melt_wide_to_long(df)` that reshapes a wide survey DataFrame (`'respondent'` plus `'q1'`, `'q2'`, `'q3'`) into long form using `pd.melt`. Requirements: - Use `id_vars='respondent'` and q1/q2/q3 as `value_vars`. - Name the new columns `'question'` (`var_name`) and `'rating'` (`value_name`). - Return columns exactly `['respondent', 'question', 'rating']`. - Keep the default `RangeIndex` and melt's row order (all q1 rows, then q2, then q3). Do not sort. Example: ```text # input (wide) respondent q1 q2 q3 0 r1 5 3 4 1 r2 2 1 5 # output (long) respondent question rating 0 r1 q1 5 1 r2 q1 2 2 r1 q2 3 3 r2 q2 1 4 r1 q3 4 5 r2 q3 5 ``` ```hint Check the order first Trace by hand the order in which `melt` emits rows for the two-respondent example, and compare it with the expected output before deciding whether you need a sort or an index reset. ``` ### What a Strong Answer Covers - Which column is the identifier and which three columns get unpivoted. - How the two new columns get their names. - The row order and index `melt` produces, and why no sort or reset is needed. - What happens to missing ratings and to any columns outside `id_vars` and `value_vars`. ### Follow-up Questions - How would you turn the long result back into the original wide shape? - The survey grows to 50 question columns named `q1` to `q50`. How do you melt them without typing each name? - How would you drop rows where the rating is missing, and what does that do to the index? - How would you replace the `question` label with an integer question number?

Overview: Reshape a wide survey DataFrame with one column per question into long form, with one row per respondent and question. It tests pd.melt arguments for identifier and value columns, naming the new columns, and the row order and index that melt produces.

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

Write melt_wide_to_long(df) that reshapes a wide survey DataFrame ('respondent' plus 'q1', 'q2', 'q3') into long form using pd.melt.

Requirements:

  • Use id_vars='respondent' and q1/q2/q3 as value_vars .
  • Name the new columns 'question' ( var_name ) and 'rating' ( value_name ).
  • Return columns exactly ['respondent', 'question', 'rating'] .
  • Keep the default RangeIndex and melt's row order (all q1 rows, then q2, then q3). Do not sort.

Example:

# input (wide)
  respondent  q1  q2  q3
0        r1   5   3   4
1        r2   2   1   5

# output (long)
  respondent question  rating
0        r1       q1       5
1        r2       q1       2
2        r1       q2       3
3        r2       q2       1
4        r1       q3       4
5        r2       q3       5

What a Strong Answer Covers Guidance

  • Which column is the identifier and which three columns get unpivoted.
  • How the two new columns get their names.
  • The row order and index melt produces, and why no sort or reset is needed.
  • What happens to missing ratings and to any columns outside id_vars and value_vars .

Follow-up Questions Guidance

  • How would you turn the long result back into the original wide shape?
  • The survey grows to 50 question columns named q1 to q50 . How do you melt them without typing each name?
  • How would you drop rows where the rating is missing, and what does that do to the index?
  • How would you replace the question label with an integer question number?