daft.DataFrame.unpivot

daft.DataFrame.unpivot#

DataFrame.unpivot(ids: Union[Expression, str, Iterable[Union[Expression, str]]], values: Union[Expression, str, Iterable[Union[Expression, str]]] = [], variable_name: str = 'variable', value_name: str = 'value') DataFrame[source]#

Unpivots a DataFrame from wide to long format.

Example

>>> import daft
>>> df = daft.from_pydict({
...     "year": [2020, 2021, 2022],
...     "Jan": [10, 30, 50],
...     "Feb": [20, 40, 60],
... })
>>> df = df.unpivot("year", ["Jan", "Feb"], variable_name="month", value_name="inventory")
>>> df = df.sort("year")
>>> df.show()
╭───────┬───────┬───────────╮
│ year  ┆ month ┆ inventory │
│ ---   ┆ ---   ┆ ---       │
│ Int64 ┆ Utf8  ┆ Int64     │
╞═══════╪═══════╪═══════════╡
│ 2020  ┆ Jan   ┆ 10        │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ 2020  ┆ Feb   ┆ 20        │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ 2021  ┆ Jan   ┆ 30        │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ 2021  ┆ Feb   ┆ 40        │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ 2022  ┆ Jan   ┆ 50        │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ 2022  ┆ Feb   ┆ 60        │
╰───────┴───────┴───────────╯

(Showing first 6 of 6 rows)
Parameters:
  • ids (ManyColumnsInputType) – Columns to keep as identifiers

  • values (Optional[ManyColumnsInputType]) – Columns to unpivot. If not specified, all columns except ids will be unpivoted.

  • variable_name (Optional[str]) – Name of the variable column. Defaults to “variable”.

  • value_name (Optional[str]) – Name of the value column. Defaults to “value”.

Returns:

Unpivoted DataFrame

Return type:

DataFrame

See also

melt