polars.Expr.struct.__getitem__#

Expr.struct.__getitem__(item: str | int) Expr[source]#

Return a struct field by name or by index.

Parameters:
item

If a string, the name of the struct field. If an integer, the index of the struct field.

Examples

Access by field name:

>>> df = pl.DataFrame({"x": [1, 2], "y": ["a", "b"]}).select(
...     pl.struct("x", "y").alias("s")
... )
>>> df.select(pl.col("s").struct["x"])
shape: (2, 1)
┌─────┐
│ x   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
│ 2   │
└─────┘

Access by field index:

>>> df.select(pl.col("s").struct[0])
shape: (2, 1)
┌─────┐
│ x   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
│ 2   │
└─────┘