Code: Select all
import polars as pl
df = pl.DataFrame({
"id": [1, 2, 3, 4, 5],
"feature_a": np.random.randint(0, 3, 5),
"feature_b": np.random.randint(0, 3, 5),
"label": [1, 0, 0, 1, 1],
})
┌─────┬───────────┬───────────┬───────┐
│ id ┆ feature_a ┆ feature_b ┆ label │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═══════════╪═══════════╪═══════╡
│ 1 ┆ 2 ┆ 0 ┆ 1 │
│ 2 ┆ 1 ┆ 1 ┆ 0 │
│ 3 ┆ 2 ┆ 2 ┆ 0 │
│ 4 ┆ 1 ┆ 0 ┆ 1 │
│ 5 ┆ 0 ┆ 0 ┆ 1 │
└─────┴───────────┴───────────┴───────┘
Code: Select all
┌─────┬───────────┬───────────┐
│ id ┆ feature_a ┆ feature_b │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═══════════╪═══════════╡
│ 1 ┆ 1 ┆ 1 │
│ 2 ┆ 0 ┆ 0 │
│ 3 ┆ 0 ┆ 0 │
│ 4 ┆ 1 ┆ 1 │
│ 5 ┆ 1 ┆ 1 │
└─────┴───────────┴───────────┘
Code: Select all
pl.col(r"^feature_.*$")
Code: Select all
pl.when(pl.col("label") == 1).then(1).otherwise(0)
Mobile version