polars.DataFrame.map_columns#
- DataFrame.map_columns(
- column_names: str | Sequence[str] | Selector,
- function: Callable[[Series], Series],
- *args: P.args,
- **kwargs: P.kwargs,
Apply eager functions to columns of a DataFrame.
Users should always prefer
with_columns()unless they are using expressions that are only possible onSeriesand not onExpr. This is almost never the case, except for a very select few functions that cannot know the output datatype without looking at the data.- Parameters:
- column_names
The columns to apply the UDF to.
- function
Callable; will receive a column series as the first parameter, followed by any given args/kwargs.
- *args
Arguments to pass to the UDF.
- **kwargs
Keyword arguments to pass to the UDF.
See also
Examples
>>> df = pl.DataFrame({"a": [1, 2, 3, 4], "b": ["10", "20", "30", "40"]}) >>> df.map_columns("a", lambda s: s.shrink_dtype()) shape: (4, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i8 ┆ str │ ╞═════╪═════╡ │ 1 ┆ 10 │ │ 2 ┆ 20 │ │ 3 ┆ 30 │ │ 4 ┆ 40 │ └─────┴─────┘
>>> df = pl.DataFrame( ... { ... "a": ['{"x":"a"}', None, '{"x":"b"}', None], ... "b": ['{"a":1, "b": true}', None, '{"a":2, "b": false}', None], ... } ... ) >>> df.map_columns(["a", "b"], lambda s: s.str.json_decode()) shape: (4, 2) ┌───────────┬───────────┐ │ a ┆ b │ │ --- ┆ --- │ │ struct[1] ┆ struct[2] │ ╞═══════════╪═══════════╡ │ {"a"} ┆ {1,true} │ │ null ┆ null │ │ {"b"} ┆ {2,false} │ │ null ┆ null │ └───────────┴───────────┘ >>> import polars.selectors as cs >>> df.map_columns(cs.all(), lambda s: s.str.json_decode()) shape: (4, 2) ┌───────────┬───────────┐ │ a ┆ b │ │ --- ┆ --- │ │ struct[1] ┆ struct[2] │ ╞═══════════╪═══════════╡ │ {"a"} ┆ {1,true} │ │ null ┆ null │ │ {"b"} ┆ {2,false} │ │ null ┆ null │ └───────────┴───────────┘