你可以如何在根据某些条件筛选的行的子集上原地编辑 Polars 数据框?

huangapple go评论57阅读模式
英文:

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 
└─────┴──────┴─────┘

huangapple
  • 本文由 发表于 2023年6月13日 17:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定