Polars [rust] 添加列,其中包含列表 [str] 系列中的计数项目

问题描述 投票:0回答:0

在将一个字符串拆分为多个“单词”之后,我想添加一个包含计数项目数量的新列

.alias("count")
.

let df = df! [
        "keys" => ["a ab", "a ab abc", "b ba abc abcd", "b ba bbc abcd bbcd"],
        "groups" => ["A", "A", "B", "C"],
    ]?;

首先我拆分字符串:

let out = df.lazy().with_column(col("keys").str().split(" "));

并尝试计数:

let out_2 = out.with_columns([col("keys")
      .apply(|s| Ok(s.len()), GetOutput::from_type(DataType::Int32))
      .alias("count")]).collect().unwrap();

导致错误消息:

mismatched types
expected struct `polars::prelude::Series`, found `usize`

不知道如何进行。

rust counting rust-polars
© www.soinside.com 2019 - 2024. All rights reserved.