melt

dataframe

Unpivots a DataFrame from wide to long format

Syntax

melt(dataframe, id_vars, value_vars?)

Parameters

dataframe (dataframe)

The DataFrame to unpivot

id_vars (array)

Columns to keep as identifier variables

value_vars (array) optional

Columns to unpivot (null means all other columns)

Returns

dataframe

Unpivoted DataFrame in long format

Examples

Convert wide format scores to long format
Input:
melt(["id", "name"], ["score1", "score2", "score3"])
Output:
Long format with id, name, variable, and value columns
Melt all columns except date
Input:
melt(["date"], null)
Output:
Long format with date preserved and all other columns melted

The melt() function unpivots a DataFrame from wide to long format. It takes identifier columns and value columns, creating a new DataFrame with one row per value.

Usage

Use melt() to convert wide format data to long format, prepare data for grouping operations, or normalize denormalized data structures. This is the inverse operation of pivot().

Related Functions