2

I have a hard time with what should be a very common use case on polars dataframes. I simply want to create a new column on an existing dataframe based on some other column. Here is the code I try, but doesn't work:

import polars as pl
df.with_columns(
                (pl.col('old_col').map_elements(lambda x: func(x)).alias("new_col"),
            )

1 Answer 1

3

Polars uses pure functions meaning that they don't have any side effects nor mutably change self.

This property will ensure that you can safely pass a DataFrame to another function without being afraid that that function changes the state of the DataFrame underneath of you.

If you want to change the assigned variable, simply reassign the new state:

df = df.with_columns(...)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.