在Python Polars中,如何根据另一列的条件将多列的值更改为null或0。

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

how to change multiple columns values to null or 0 based on another column condition in python polars

问题

你可以使用Python Polars根据另一列的条件来为多个列分配相同的值。在这种情况下,基于列"A"的值是否为"a",然后将列"B"和"C"的值设置为0。以下是示例代码:

import polars as pl

df = pl.DataFrame({
   "A": ["a", "b", "a", "c"], 
   "B": [23, 45, 60, 50], 
   "C": [11, 26, 63, 84]
})

# 根据条件将列"B"和"C"的值设置为0
df = df.with_column(
    pl.when(df["A"] == "a")
    .then(0)
    .otherwise(df["B"])
    .alias("B")
)

df = df.with_column(
    pl.when(df["A"] == "a")
    .then(0)
    .otherwise(df["C"])
    .alias("C")
)

print(df)

这将产生以下输出,其中列"B"和"C"的值根据列"A"的条件进行了设置:

shape: (4, 3)
┌─────┬─────┬─────┐
│ A   ┆ B   ┆ C   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ a   ┆ 0   ┆ 0   │
│ b   ┆ 45  ┆ 26  │
│ a   ┆ 0   ┆ 0   │
│ c   ┆ 50  ┆ 84  │
└─────┴─────┴─────┘

这段代码使用了pl.whenpl.otherwise来根据条件设置列的值。如果"A"列的值是"a",则将"B"和"C"列的值设置为0,否则保持原始值。

英文:

how to assign same value to multiple columns based on another column condition in python polars

df = pl.DataFrame({
   "A": ["a", "b", "a", "c"], 
   "B": [23,45,60,50], 
   "C": [11, 26, 63, 84]
})

┌─────┬─────┬─────┐
│ A   ┆ B   ┆ C   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ a   ┆ 23  ┆ 11  │
│ b   ┆ 45  ┆ 26  │
│ a   ┆ 60  ┆ 63  │
│ c   ┆ 50  ┆ 84  │
└─────┴─────┴─────┘


based on column A if value is "a" , then value for column B and C need to be set as 0

need output as

┌─────┬─────┬─────┐
│ A   ┆ B   ┆ C   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ a   ┆ 0   ┆ 0   │
│ b   ┆ 45  ┆ 26  │
│ a   ┆ 0   ┆ 0   │
│ c   ┆ 50  ┆ 84  │
└─────┴─────┴─────┘

答案1

得分: 3

df.with_columns(
   pl.when(pl.col("A") != "a")
     .then(pl.col("B", "C"))
     .otherwise(0)
)
shape: (4, 3)
┌─────┬─────┬─────┐
 A    B    C   
 ---  ---  --- 
 str  i64  i64 
╞═════╪═════╪═════╡
 a    0    0   
 b    45   26  
 a    0    0   
 c    50   84  
└─────┴─────┴─────┘

在这个示例中,你还可以使用.then(pl.exclude("A"))来代替命名剩余的列。

英文:

https://stackoverflow.com/questions/75211934/how-can-i-use-when-then-and-otherwise-with-multiple-conditions-in-polars

df.with_columns(
   pl.when(pl.col("A") != "a")
     .then(pl.col("B", "C"))
     .otherwise(0)
)
shape: (4, 3)
┌─────┬─────┬─────┐
│ A   ┆ B   ┆ C   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ a   ┆ 0   ┆ 0   │
│ b   ┆ 45  ┆ 26  │
│ a   ┆ 0   ┆ 0   │
│ c   ┆ 50  ┆ 84  │
└─────┴─────┴─────┘

For this example you could also use .then(pl.exclude("A")) instead of having to name the remaining columns.

huangapple
  • 本文由 发表于 2023年4月4日 11:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925405.html
匿名

发表评论

匿名网友

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

确定