英文:
How can I edit a polars dataframe in place on a subset of rows that I am filtering according to some condition?
问题
你可以尝试以下方法来在原始数据框上编辑符合某个条件的子集行:
df[(df['a'] == 1)]['c'] = 'baz'
这将会将符合条件的行中列 c
的值替换为 baz
,并且会在原始数据框上进行修改。
英文:
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"
答案1
得分: 4
你应该使用 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 │
└─────┴──────┴─────┘
英文:
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 │
└─────┴──────┴─────┘
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论