polars.Expr.flatten#

Expr.flatten() Expr[source]#

Alias for explode().

Explode a list or utf8 Series. This means that every item is expanded to a new row.

Returns:
Exploded Series of same dtype

Examples

The following example turns each character into a separate row:

>>> df = pl.DataFrame({"foo": ["hello", "world"]})
>>> (df.select(pl.col("foo").flatten()))
shape: (10, 1)
┌─────┐
│ foo │
│ --- │
│ str │
╞═════╡
│ h   │
├╌╌╌╌╌┤
│ e   │
├╌╌╌╌╌┤
│ l   │
├╌╌╌╌╌┤
│ l   │
├╌╌╌╌╌┤
│ ... │
├╌╌╌╌╌┤
│ o   │
├╌╌╌╌╌┤
│ r   │
├╌╌╌╌╌┤
│ l   │
├╌╌╌╌╌┤
│ d   │
└─────┘

This example turns each word into a separate row:

>>> df = pl.DataFrame({"foo": ["hello world"]})
>>> (df.select(pl.col("foo").str.split(by=" ").flatten()))
shape: (2, 1)
┌───────┐
│ foo   │
│ ---   │
│ str   │
╞═══════╡
│ hello │
├╌╌╌╌╌╌╌┤
│ world │
└───────┘