最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to format expr in polars by rust? - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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% │
└───────┘
发布评论

评论列表(0)

  1. 暂无评论