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"
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 │
└─────┴──────┴─────┘
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With