How do you convert between wide and long formats using pivot() versus melt(), and how are they inverses?

Quick Overview

Asks how to reshape a pandas DataFrame from long to wide with pivot() and back to long with melt(), and in what sense the two are inverses. Tests the arguments each function takes, how repeated keys are handled, and what a round trip does and does not preserve.

How do you convert between wide and long formats using pivot() versus melt(), and how are they inverses?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

In pandas, how do you convert a DataFrame between wide and long formats using `pivot()` and `melt()`? Explain which arguments each function takes and in what sense the two operations are inverses of each other. ```hint Think about one cell In long format each row is one (id, variable, value) triple. Ask what has to be true about those triples for every one of them to land in exactly one cell of a wide table, and what a wide cell with no matching triple turns into. ``` ### What a Strong Answer Covers - The direction of each function and the arguments that control it: `index`, `columns` and `values` for `pivot()`; `id_vars`, `value_vars`, `var_name` and `value_name` for `melt()`. - What happens when the long data has repeated index/column pairs, and where `pivot_table()` fits in. - A round trip in code that gets back to the starting shape. - The conditions under which the inverse is exact, and what changes otherwise (row order, axis names, missing combinations, dtypes). ### Follow-up Questions - How would you do the same reshapes with `set_index()` plus `unstack()` and `stack()`, and when is that more convenient? - Your long table has two readings for some (date, city) pairs. Which aggregation would you choose in `pivot_table()`, and what does that do to the round trip? - How would you reshape columns such as `sales_2023`, `sales_2024`, `units_2023` and `units_2024` into a long table with a year column? - After a pivot, some cities have no reading on some dates. How do those gaps show up, and how do you handle them when melting back?

Overview: Asks how to reshape a pandas DataFrame from long to wide with pivot() and back to long with melt(), and in what sense the two are inverses. Tests the arguments each function takes, how repeated keys are handled, and what a round trip does and does not preserve.

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

In pandas, how do you convert a DataFrame between wide and long formats using pivot() and melt()? Explain which arguments each function takes and in what sense the two operations are inverses of each other.

What a Strong Answer Covers Guidance

  • The direction of each function and the arguments that control it: index , columns and values for pivot() ; id_vars , value_vars , var_name and value_name for melt() .
  • What happens when the long data has repeated index/column pairs, and where pivot_table() fits in.
  • A round trip in code that gets back to the starting shape.
  • The conditions under which the inverse is exact, and what changes otherwise (row order, axis names, missing combinations, dtypes).

Follow-up Questions Guidance

  • How would you do the same reshapes with set_index() plus unstack() and stack() , and when is that more convenient?
  • Your long table has two readings for some (date, city) pairs. Which aggregation would you choose in pivot_table() , and what does that do to the round trip?
  • How would you reshape columns such as sales_2023 , sales_2024 , units_2023 and units_2024 into a long table with a year column?
  • After a pivot, some cities have no reading on some dates. How do those gaps show up, and how do you handle them when melting back?