2

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?

1 Answer 1

3

You 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% │
└───────┘
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.