Question
How can I efficiently `fill_null` only certain columns of a DataFrame?
For example, let us say I want to fill_null(strategy="zero")
only the numeric columns of my DataFrame. My current strategy is to do this:
import polars as pl
import polars.selectors as cs
df = pl.DataFrame(
[
pl.Series("id", ["alpha", None, "gamma"]),
pl.Series("xs", [None, 100, 2]),
]
)
final_df = df.select(cs.exclude(cs.numeric()))
final_df = final_df.with_columns(
df.select(cs.numeric()).fill_null(strategy="zero")
)
print(final_df)
shape: (3, 2)
┌───────┬─────┐
│ id ┆ xs │
│ --- ┆ --- │
│ str ┆ i64 │
╞═══════╪═════╡
│ alpha ┆ 0 │
│ null ┆ 100 │
│ gamma ┆ 2 │
└───────┴─────┘
Are there alternative, either more idiomatic or more efficient methods to achieve what I'd like to do?
2 86
2