You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust Polars:eager模式字符串过滤实现及col/column区别咨询

在Polars Eager模式下实现字符串过滤及col/column用法解析

一、Eager模式下实现字符串contains过滤

你在Lazy模式中的代码是保留A列所有行,同时对B列做条件过滤(仅保留包含input的行,其余行的B列值为null)。对应Eager模式的实现如下:

// 获取B列的字符串匹配掩码
let mask = df.column("B")?.str()?.contains(input)?;
// 生成过滤后的B列:符合条件保留原值,否则为null
let filtered_b = df.column("B")?.mask(&mask)?;
// 组合A列与过滤后的B列,取前10行
let out = df
    .select(["A"])?
    .with_column(filtered_b.alias("B"))?
    .head(Some(10));

如果你的实际需求是过滤整个DataFrame的行(仅保留B列包含input的行,同时保留A、B完整列数据),Eager模式写法更简洁:

let mask = df.column("B")?.str()?.contains(input)?;
let out = df.filter(&mask)?.select(["A", "B"])?.head(Some(10));

二、col("")与column("")的适用范围

你的理解不准确:

  • col("col_name")是Polars的表达式(Expr)构造器,核心用于Lazy模式构建查询计划,但也可在Eager模式的select、with_column等方法中使用,比如:
    // Eager模式下用col选择列
    let df_eager = df.select([col("A"), col("B")])?;
    
  • column("col_name")是直接从已加载的DataFrame中获取对应Series,属于纯Eager操作,返回具体数据而非查询表达式,仅适用于Eager流程。

简单总结:col是操作逻辑的表达式描述,可跨Lazy/Eager模式使用;column是直接提取数据,仅适用于Eager模式。

内容的提问来源于stack exchange,提问作者fvg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 12:15:32