Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit a polars dataframe in place on a subset of rows that I am filtering according to some condition?

How can I edit a dataframe in place on a subset of rows that I am filtering according to some condition?

So I have an example like this:

df = pl.DataFrame(
    {
        "a": [0, 1, 2, 3],
        "b": [0.5, 4, 10, 13],
        "c": ["foo", "foo", "foo", "foo"],
    }
)

and I want to replace the value in col c with baz, but only if the value in col a is 1.

I can select the row with filter and edit that with with_columns, but then I have a new df.

df.filter(
    pl.col("a") == 1
).with_columns(
    pl.col("c").str.replace("foo", "baz")
)

How do I get back the original df, with the modified row?

a    b    c
i64    f64    str
0    0.5    "foo"
1    4.0    "baz"
2    10.0    "foo"
3    13.0    "foo"
like image 854
RCap107 Avatar asked Dec 22 '25 09:12

RCap107


1 Answers

You should use pl.when:

df.with_columns(
    pl.when(pl.col("a").eq(1)).then(pl.col("c").str.replace("foo", "bar")).otherwise(pl.col("c"))
)

shape: (4, 3)
┌─────┬──────┬─────┐
│ a   ┆ b    ┆ c   │
│ --- ┆ ---  ┆ --- │
│ i64 ┆ f64  ┆ str │
╞═════╪══════╪═════╡
│ 0   ┆ 0.5  ┆ foo │
│ 1   ┆ 4.0  ┆ bar │
│ 2   ┆ 10.0 ┆ foo │
│ 3   ┆ 13.0 ┆ foo │
└─────┴──────┴─────┘
like image 118
baggiponte Avatar answered Dec 23 '25 23:12

baggiponte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!