Start studying
Module 6 · Data Wrangling Module contents
Module 6  ·  Card 14 / 14advanced

Melting Wide to Long

Melting turns a wide table, with one column per category, into a long one with a row per value.

It is the reverse of pivoting, and in pandas it is done with the melt function.

For example, a table with a column for each month melts into rows of store, month and revenue.

The id_vars option names the columns to keep as they are, and value_vars names the columns to fold into rows.

Data often arrives wide from spreadsheets, so melting is often the first step towards tidy data.

Learn in depth ↓
Module 06 · Data Wrangling · 14 concepts
In depth  /  15 lines

Melting Wide to Long, in more detail

Spreadsheets favour wide layouts, because they are easy to type into and read across.

Analysis tools favour long layouts, because one column per variable makes grouping and filtering simple.

Melting bridges the two by folding a set of columns into a pair of columns.

One new column holds the old column names, such as month, and the other holds the values, such as revenue.

The var_name and value_name options set those two names, which otherwise default to variable and value.

A table with 50 stores and 12 month columns melts into 600 rows.

The kept columns repeat on every new row, which is why long tables are taller and look repetitive.

Column headers often hide two variables, such as revenue_2023 and revenue_2024, which need splitting after melting.

Splitting the new column with str.split or str.extract separates them into proper columns.

Melted headers are text, so a month column usually needs converting to a date or an ordered category.

Blank cells in the wide table become rows with blank values, which can be dropped if they mean nothing was recorded.

The wide_to_long function handles headers with a shared stem and a numbered ending, common in survey data.

The stack method does a similar job when the columns already sit in a layered index.

In practice melt is often the first step after reading a spreadsheet export.

The usual mistake is leaving the identifier out of id_vars, so the values can no longer be traced to their row.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
A spreadsheet has columns Store, Jan, Feb and Mar holding revenue. To get one row per store and month, what should melt keep fixed?
Data Wrangling

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close