narwhals.Expr.map
get
get(key: NonNestedLiteral) -> ExprT
Return the value for key in each map.
The result is null where the map is null or does not contain key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
NonNestedLiteral
|
Key to look up. Its type should match the map's key type. |
required |
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>> df_native = pl.DataFrame(
... {
... "a": pl.Series(
... [{"x": 1, "y": 2}, {"y": 3}, None],
... dtype=pl.Map(pl.String, pl.Int64),
... )
... }
... )
>>> df = nw.from_native(df_native)
>>> df.with_columns(a_x=nw.col("a").map.get("x"))
┌───────────────────────────┐
| Narwhals DataFrame |
|---------------------------|
|shape: (3, 2) |
|┌──────────────────┬──────┐|
|│ a ┆ a_x │|
|│ --- ┆ --- │|
|│ map[str, i64] ┆ i64 │|
|╞══════════════════╪══════╡|
|│ {"x": 1, "y": 2} ┆ 1 │|
|│ {"y": 3} ┆ null │|
|│ null ┆ null │|
|└──────────────────┴──────┘|
└───────────────────────────┘