polars.internals.expr.ExprListNameSpace.to_struct

ExprListNameSpace.to_struct(n_field_strategy: str = 'first_non_null', name_generator: Optional[Callable[[int], str]] = None) polars.internals.expr.Expr

Convert the series of type List to a series of type Struct.

Parameters
n_field_strategy

Strategy to determine the number of fields of the struct. Any of {‘first_non_null’, ‘max_width’}

name_generator

A custom function that can be used to generate the field names. Default field names are field_0, field_1 .. field_n

Examples

>>> df = pl.DataFrame({"a": [[1, 2, 3], [1, 2]]})
>>> df.select([pl.col("a").arr.to_struct()])
shape: (2, 1)
┌────────────┐
│ a          │
│ ---        │
│ struct[3]  │
╞════════════╡
│ {1,2,3}    │
├╌╌╌╌╌╌╌╌╌╌╌╌┤
│ {1,2,null} │
└────────────┘
>>> df.select(
...     [
...         pl.col("a").arr.to_struct(
...             name_generator=lambda idx: f"col_name_{idx}"
...         )
...     ]
... ).to_series().to_list()
[{'col_name_0': 1, 'col_name_1': 2, 'col_name_2': 3},
{'col_name_0': 1, 'col_name_1': 2, 'col_name_2': None}]