Код: Выделить всё
s = pd.Series(['a', 'b', 'c', 'd', 'e'])
idx = [True, False, True, False, True]
s[idx] = ['x', 'y', 'z']
Код: Выделить всё
s = pl.Series(['a', 'b', 'c', 'd', 'e'])
idx = pl.Series([True, False, True, False, True])
s = s.scatter(idx.arg_true(), ['x', 'y', 'z']) # arg_true() gives us integer indices
Код: Выделить всё
import polars as pl
from polars import col, when
df = pl.DataFrame({
"my_col": ['a', 'b', 'c', 'd', 'e'],
"idx": [True, False, True, False, True]
})
new_values = ['x', 'y', 'z']
df = df.with_columns(
when(col("idx")).then(new_values) # how to do this?
.otherwise(col("my_col"))
)
Код: Выделить всё
s = df["my_col"].clone()
s = s.scatter(df["idx"].arg_true(), new_values)
df = df.with_columns(s.alias("my_col"))
Код: Выделить всё
shape: (5, 2)
┌────────┬───────┐
│ my_col ┆ idx │
│ --- ┆ --- │
│ str ┆ bool │
╞════════╪═══════╡
│ x ┆ true │
│ b ┆ false │
│ y ┆ true │
│ d ┆ false │
│ z ┆ true │
└────────┴───────┘
Подробнее здесь: https://stackoverflow.com/questions/737 ... in-an-expr
Мобильная версия