polars.Expr.list.to_struct#
- Expr.list.to_struct(fields: Sequence[str]) Expr[source]#
Convert the Series of type
Listto a Series of typeStruct.- Parameters:
- fields
Field names to use for the output. The number of names determines how many fields will be in the output.
Examples
>>> df = pl.DataFrame( ... { ... "c": pl.Series( ... [ ... [1], ... [0, 1], ... [1, 0, 1], ... [], ... [None, 1], ... None, ... ], ... ) ... } ... ) >>> print( ... result := df.with_columns( ... c_struct=pl.col("c").list.to_struct(["x", "y"]), ... ) ... ) shape: (6, 2) ┌───────────┬─────────────┐ │ c ┆ c_struct │ │ --- ┆ --- │ │ list[i64] ┆ struct[2] │ ╞═══════════╪═════════════╡ │ [1] ┆ {1,null} │ │ [0, 1] ┆ {0,1} │ │ [1, 0, 1] ┆ {1,0} │ │ [] ┆ {null,null} │ │ [null, 1] ┆ {null,1} │ │ null ┆ null │ └───────────┴─────────────┘ >>> print(result["c_struct"].struct.unnest()) shape: (6, 2) ┌──────┬──────┐ │ x ┆ y │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞══════╪══════╡ │ 1 ┆ null │ │ 0 ┆ 1 │ │ 1 ┆ 0 │ │ null ┆ null │ │ null ┆ 1 │ │ null ┆ null │ └──────┴──────┘