Pandasdrill

Combining and reshaping Updated November 2026

melt

df.melt(id_vars=["id"], value_vars=["jan", "feb"], var_name="month", value_name="sales") unpivots wide columns into two long columns: one holding the former column name and one holding the value. It is the inverse of pivot.

Syntax

df.melt(id_vars=None, value_vars=None, var_name=None, value_name="value")
PartWhat it is
id_varsColumns to keep as identifiers (repeated for each melted column)
value_varsColumns to unpivot; default is all columns not in id_vars
var_nameName for the column that holds the former column names (default "variable")
value_nameName for the values column (default "value")

Examples

import pandas as pd

wide = pd.DataFrame({"store": ["N", "S"],
                     "jan": [100, 80],
                     "feb": [120, 90]})
wide.melt(id_vars="store", var_name="month", value_name="sales")

Result: store month sales 0 N jan 100 1 S jan 80 2 N feb 120 3 S feb 90

wide.melt(id_vars="store", value_vars=["feb"])

Result: store variable value 0 N feb 120 1 S feb 90

Only the listed value_vars are melted; defaults give the names variable and value.

long = wide.melt(id_vars="store", var_name="month", value_name="sales")
long.pivot(index="store", columns="month", values="sales")

Result: month feb jan store N 120 100 S 90 80

pivot reverses melt; columns come out sorted alphabetically.

Common mistakes

  • The id column got melted too, showing store as a variable: list it in id_vars.
  • Mixed types in the value column (numbers and strings) because a text column was melted: restrict value_vars to the numeric columns.
  • Values arrive as object dtype after melting columns of different dtypes; cast with pd.to_numeric afterwards.

Practice melt

These drills check your answer on live data, free, no account needed.

  • Wide to long with meltTurn `wide` (one column per quarter) into a long table with the columns `store`, `quarter` and `sales`.
  • Columns into rows with stack`wide` is indexed by store with one column per quarter. Use `stack` to turn it into a Series indexed by store and quarter.

Questions

What is the opposite of melt?

pivot (no aggregation) or pivot_table (with aggregation) turns long data back into wide.

When should I use long format?

Long (tidy) format works best for groupby, plotting libraries like seaborn and plotly, and for data with many measurement columns. Wide format is for reading and reporting.

Related

  • pivotHow df.pivot works in pandas: turn a long table with key and value columns into a wide table, what index, columns and values mean, and the duplicates error.
  • pivot_tableHow to build a pivot table in pandas with pivot_table: index, columns, values, aggfunc, fill_value for empty cells, margins for totals, several aggregations.
  • concatHow to use pd.concat in pandas to append rows from several DataFrames, add columns side by side with axis=1, reset the index, and label the source with keys.
  • groupbyHow groupby works in pandas: group by one or more columns, apply sum, mean, count or size, get a flat table with as_index=False, and iterate over groups.

Previous: concatNext: pivot