Polars python, format a column like this
df = pl.DataFrame({
"a": [0.15, 0.25]
})
result = df.with_columns(
pl.format("{}%", (pl.col("a") * 100).round(1))
)
print(result)
output
shape: (2, 1)
┌───────┐
│ a │
│ --- │
│ str │
╞═══════╡
│ 15.0% │
│ 25.0% │
└───────┘
How to do this with polars rust?
Polars python, format a column like this
df = pl.DataFrame({
"a": [0.15, 0.25]
})
result = df.with_columns(
pl.format("{}%", (pl.col("a") * 100).round(1))
)
print(result)
output
shape: (2, 1)
┌───────┐
│ a │
│ --- │
│ str │
╞═══════╡
│ 15.0% │
│ 25.0% │
└───────┘
How to do this with polars rust?
Share Improve this question edited Mar 11 at 15:39 jqurious 22.1k5 gold badges20 silver badges39 bronze badges asked Mar 11 at 15:23 NyssanceNyssance 4115 silver badges9 bronze badges1 Answer
Reset to default 3You can format it using format_str
. You must also enable lazy
, concat_str
, strings
, round_series
features in Cargo.toml
file.
use polars::prelude::*;
fn main() {
let mut df = df!(
"a" => &[0.15, 0.25]
)
.unwrap();
let format_str = [format_str("{}%", [(col("a") * lit(100)).round(1)]).unwrap()];
let new = df.lazy().with_columns(format_str).collect().unwrap();
println!("{:?}", new);
}
This will give you the following output.
┌───────┐
│ a │
│ --- │
│ str │
╞═══════╡
│ 15.0% │
│ 25.0% │
└───────┘