Reshaping: melt, stack, unstack

Reshaping in pandas is the act of changing a table's layout without changing its data — converting between wide format (one column per variable) and long format (one observation per row) using melt , stack , and unstack so the data matches what your next analysis step expects.

Learn Reshaping: melt, stack, unstack in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A wide table like this — one column per month — is easy to read but awkward to analyse. pd.melt unpivots the month columns into two tidy columns: one holding the variable name, one holding the value. The columns you keep fixed go in id_vars .

df.stack() pushes the column labels down into the row index (wide → long), and df.unstack() pulls an index level up into the columns (long → wide). They are inverses of each other.

To go the other direction — from tidy long data back to a wide layout — use df.pivot . It is the inverse of melt : the variable column becomes the new column headers.

You need tidy data for grouping, plotting, or modelling.

You want a compact, human-readable report or matrix.

Unpivot the quarter columns into tidy rows, keeping company as the identifier. Fill in the blank argument.

Take a wide table, melt it to long, compute the average per subject, then pivot a summary back to wide.

Lesson complete — you can reshape data at will!

You understand wide vs long, can melt and stack to tidy your data, and pivot or unstack to summarise it back into a readable grid. Reshaping is the glue between messy raw data and clean analysis.

🚀 Up next: MultiIndex (Hierarchical Indexing) — work with indexes that have more than one level.

Practice quiz

Which function turns wide columns into long rows?

  • pd.melt()
  • pd.widen()
  • pd.spread()
  • pd.cast()

Answer: pd.melt(). pd.melt() unpivots columns into variable/value rows (wide to long).

What are the default column names melt() creates?

  • 'key' and 'val'
  • 'variable' and 'value'
  • 'name' and 'data'
  • 'col' and 'num'

Answer: 'variable' and 'value'. melt produces a 'variable' column and a 'value' column by default.

Which method reshapes rows back into columns from a MultiIndex?

  • df.melt()
  • df.stack()
  • df.unstack()
  • df.flatten()

Answer: df.unstack(). unstack() pivots an index level out into columns.

What does df.stack() return for a simple DataFrame?

  • A dict
  • A list of rows
  • Another DataFrame
  • A Series with a MultiIndex

Answer: A Series with a MultiIndex. stack() compresses columns into the index, yielding a Series.

Which method reshapes using index, columns, and values arguments?

  • df.pivot(index=..., columns=..., values=...)
  • df.reshape()
  • df.spread()
  • df.transpose()

Answer: df.pivot(index=..., columns=..., values=...). pivot() builds a new table from index/columns/values.

What happens if pivot() finds duplicate index/column pairs?

  • It averages them silently
  • It raises a ValueError
  • It keeps the last one
  • It drops the column

Answer: It raises a ValueError. pivot() cannot handle duplicates and raises ValueError; use pivot_table.

Which function aggregates duplicates while reshaping?

  • pd.melt()
  • df.stack()
  • pd.pivot_table()
  • df.explode()

Answer: pd.pivot_table(). pivot_table() applies an aggfunc, handling duplicate keys.

In melt(), what does the id_vars argument do?

  • Names the value column
  • Sets the aggregation
  • Sorts the output
  • Lists columns to keep fixed as identifiers

Answer: Lists columns to keep fixed as identifiers. id_vars are the columns kept as-is; the rest are unpivoted.

What is the inverse operation of stack()?

  • unstack()
  • melt()
  • pivot()
  • transpose()

Answer: unstack(). unstack() reverses stack(), moving an index level back to columns.

Going from a wide table (one column per month) to one row per month is called what?

  • Pivoting wider
  • Wide to long (melting)
  • Transposing only
  • Stacking columns

Answer: Wide to long (melting). Collapsing many columns into rows is the wide-to-long transform done by melt.