Skip to content

narwhals.Expr.struct

field

field(name: str) -> ExprT

Retrieve a Struct field as a new expression.

Parameters:

Name Type Description Default
name str

Name of the struct field to retrieve.

required

Returns:

Type Description
ExprT

A new expression.

Examples:

>>> import polars as pl
>>> import narwhals as nw
>>> df_native = pl.DataFrame(
...     {"user": [{"id": "0", "name": "john"}, {"id": "1", "name": "jane"}]}
... )
>>> df = nw.from_native(df_native)
>>> df.with_columns(name=nw.col("user").struct.field("name"))
┌───────────────────────┐
|  Narwhals DataFrame   |
|-----------------------|
|shape: (2, 2)          |
|┌──────────────┬──────┐|
|│ user         ┆ name │|
|│ ---          ┆ ---  │|
|│ struct[2]    ┆ str  │|
|╞══════════════╪══════╡|
|│ {"0","john"} ┆ john │|
|│ {"1","jane"} ┆ jane │|
|└──────────────┴──────┘|
└───────────────────────┘