polars.Series.arr.to_struct#

Series.arr.to_struct(n_field_strategy: ToStructStrategy = 'first_non_null', name_generator: Callable[[int], str] | None = None) Series[source]#

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

Parameters:
n_field_strategy{‘first_non_null’, ‘max_width’}

Strategy to determine the number of fields of the struct. ‘first_non_null’: set number of fields to the length of the first non-zero-length sublist. ‘max_width’: set number of fields as max length of all sublists.

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}]