variableName,
valueName.
DataFrame unpivot(DataFrame df, string[] valueColumns,
string[] indexColumns = [], string variableName = "variable",
string valueName = "value")Unpivots the frame: the valueColumns stack into variableName/valueName pairs (value-column outer, input row inner), the indexColumns repeat per stacked column, and every other column is dropped - visible in the signature, Polars 2.0 parity (ADR-2026-10-24-20).
The stacked value dtype keeps identical dtypes and otherwise joins in the numeric lattice of ADR-2026-10-24-19 (all-integer INT64 exact widening, any float FLOAT64, UINT64 meeting a signed integer FLOAT64 with the approximation disclosed); any other mix rejects with an error naming the pair - no silent stringification. Nulls stack as nulls. Zero rows or zero value columns produce zero rows with the variable/value (and index) columns present.
df | the frame to reshape |
valueColumns | the columns to stack (in output order) |
indexColumns | the columns to repeat per stacked column |
variableName | the stacked column-name column |
valueName | the stacked values column |
variableName,
valueName.
names.
Example:
auto df = DataFrame([
Series("i", [1, 2]),
Series("a", [10L, 20]),
Series("b", [30L, 40]),
]);
auto tall = unpivot(df, ["a", "b"], ["i"]);
assert(tall.columns() == ["i", "variable", "value"]);
assert(tall["variable"].toArray!string() == ["a", "a", "b", "b"]);
assert(tall["value"].toArray!long() == [10, 20, 30, 40]);