Edit DF Column

Note

Using lambda func with map_elements(), or appl(), is much slower than using native expressions.

1 add dummy dataframe

import polars as pl
from datetime import datetime

df = pl.DataFrame(
    {
        "integer": [1, 2, 3],
        "date": [
            datetime(2025, 1, 1),
            datetime(2025, 1, 2),
            datetime(2025, 1, 3),
        ],
        "float": [4.0, 5.0, 6.0],
        "string": ["a", "b", "c"],
    }
)

df
shape: (3, 4)
integer date float string
i64 datetime[μs] f64 str
1 2025-01-01 00:00:00 4.0 "a"
2 2025-01-02 00:00:00 5.0 "b"
3 2025-01-03 00:00:00 6.0 "c"

2 use lambda func to edit a string column

lambda_add_str = lambda str: str + "+"

df = df.with_columns(
    pl.col('string')
        .map_elements(lambda_add_str, return_dtype=pl.String)
        .alias('str_plus')
)

df
/var/folders/7g/c9hb4wgd0mz62mwd00xdd4q40000gn/T/ipykernel_10365/3168615590.py:5: PolarsInefficientMapWarning:


Expr.map_elements is significantly slower than the native expressions API.
Only use if you absolutely CANNOT implement your logic otherwise.
Replace this expression...
  - pl.col("string").map_elements(lambda str: ...)
with this one instead:
  + pl.col("string") + '+'

shape: (3, 5)
integer date float string str_plus
i64 datetime[μs] f64 str str
1 2025-01-01 00:00:00 4.0 "a" "a+"
2 2025-01-02 00:00:00 5.0 "b" "b+"
3 2025-01-03 00:00:00 6.0 "c" "c+"
# alternativly...

df = df.with_columns(
    (pl.col('string') + "-").alias('str_minus')
)

df
shape: (3, 6)
integer date float string str_plus str_minus
i64 datetime[μs] f64 str str str
1 2025-01-01 00:00:00 4.0 "a" "a+" "a-"
2 2025-01-02 00:00:00 5.0 "b" "b+" "b-"
3 2025-01-03 00:00:00 6.0 "c" "c+" "c-"
Back to top