polars.DataFrame.explode#
- DataFrame.explode(
- columns: ColumnNameOrSelector | Iterable[ColumnNameOrSelector],
- *more_columns: ColumnNameOrSelector,
- empty_as_null: bool = False,
- keep_nulls: bool = True,
Explode the dataframe to long format by exploding the given columns.
- Parameters:
- columns
Column names, expressions, or a selector defining them. The underlying columns being exploded must be of the
ListorArraydata type.- *more_columns
Additional names of columns to explode, specified as positional arguments.
- empty_as_null
Explode an empty list/array into a
null.- keep_nulls
Explode a
nulllist/array into anull.
- Returns:
- DataFrame
Examples
>>> df = pl.DataFrame( ... { ... "letters": ["a", "a", "b", "c"], ... "numbers": [[1], [2, 3], [4, 5], [6, 7, 8]], ... } ... ) >>> df shape: (4, 2) โโโโโโโโโโโฌโโโโโโโโโโโโ โ letters โ numbers โ โ --- โ --- โ โ str โ list[i64] โ โโโโโโโโโโโชโโโโโโโโโโโโก โ a โ [1] โ โ a โ [2, 3] โ โ b โ [4, 5] โ โ c โ [6, 7, 8] โ โโโโโโโโโโโดโโโโโโโโโโโโ >>> df.explode("numbers") shape: (8, 2) โโโโโโโโโโโฌโโโโโโโโโโ โ letters โ numbers โ โ --- โ --- โ โ str โ i64 โ โโโโโโโโโโโชโโโโโโโโโโก โ a โ 1 โ โ a โ 2 โ โ a โ 3 โ โ b โ 4 โ โ b โ 5 โ โ c โ 6 โ โ c โ 7 โ โ c โ 8 โ โโโโโโโโโโโดโโโโโโโโโโ