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

如何通过判断列值是否存在于向量中来过滤Polars DataFrame?

问题

我有一个包含“ID”列(数据类型为UInt32)的DataFrame,还有一个名为ids的向量。我希望返回仅包含“ID”列值存在于ids向量中的行的DataFrame。

最小期望示例

use polars::df;
use polars::prelude::*;

fn filter_by_id(table: &DataFrame, ids: Vec<u32>) -> DataFrame {
    df!{
        "ID" => &[1, 3, 5],
        "VALUE" => &["B", "D", "F"]
    }.unwrap()
}

fn main() {
    let table = df!{
        "ID" => &[0, 1, 2, 3, 4, 5],
        "VALUE" => &["A", "B", "C", "D", "E", "F"]
    }.unwrap();
    let ids = vec![1, 3, 5];
    let filtered_table = filter_by_id(&table, ids);
    println!("{:?}", table);
    println!("{:?}", filtered_table);
}

输入DataFrame

IDVALUE
0A
1B
2C
3D
4E
5F

过滤向量 = [1, 3, 5]

期望输出DataFrame

IDVALUE
1B
3D
5F

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:40:52