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.