Код: Выделить всё
df = pl.DataFrame(
{
"A": ["foo", "ham", "spam", "egg",],
"L": ["A54", "A12", "B84", "C12"],
"G": ["X34", "C84", "G96", "L6",],
}
)
print(df)
shape: (4, 3)
┌──────┬─────┬─────┐
│ A ┆ L ┆ G │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞══════╪═════╪═════╡
│ foo1 ┆ A54 ┆ X34 │
│ ham ┆ A12 ┆ C84 │
│ foo2 ┆ B84 ┆ G96 │
│ egg ┆ C12 ┆ L6 │
└──────┴─────┴─────┘
expected outcome
shape: (4, 3)
┌──────┬─────┬─────┐
│ A ┆ L ┆ G │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞══════╪═════╪═════╡
│ foo1 ┆ X34 ┆ X34 │
│ ham ┆ A12 ┆ C84 │
│ foo2 ┆ G96 ┆ G96 │
│ egg ┆ C12 ┆ L6 │
└──────┴─────┴─────┘
Код: Выделить всё
df = df.with_columns(
pl.when(
pl.col("A")
.str.contains("foo"))
.then(pl.col("L"))
.alias("G")
.otherwise(pl.col("G"))
)
Подробнее здесь: https://stackoverflow.com/questions/755 ... -in-polars