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

如何用NumPy数组替换Polars DataFrame指定分组的元素?

问题描述

现有Polars DataFrame:

import polars as pl
import numpy as np

df = pl.DataFrame({'group': [1, 1, 1, 3, 3, 3, 4, 4]})

希望将group列中值为3的元素替换为NumPy数组:

values = np.array([9, 8, 7])

预期结果如下:

shape: (8, 1)
┌───────┐
│ group │
│ ---   │
│ i64   │
╞═══════╡
│ 1     │
│ 1     │
│ 1     │
│ 9     │
│ 8     │
│ 7     │
│ 4     │
│ 4     │
└───────┘

尝试以下代码时出现ShapeError: shapes of self, mask and other are not suitable for zip_with operation报错:

(
    df
    .with_columns(
        pl.when(pl.col('group')==3)
        .then(values)
        .otherwise(pl.col('group'))
    ).alias('group')
)

请问如何正确实现该需求?

解决方案

原代码报错的核心原因是:直接在pl.when().then()中传入NumPy数组时,Polars无法将数组元素与原列中group==3的单个行一一匹配——虽然两者元素数量一致,但数组会被当成整体处理,而非逐个映射到对应行。

以下两种方法可以实现需求:

方法一:用pl.Series包装数组后匹配替换

将目标数组转为Polars Series,让when/then能按位置对应替换:

# 把numpy数组转为Polars Series
replace_series = pl.Series(values)
# 标记需要替换的行
mask = df['group'] == 3

result_df = df.with_columns(
    pl.when(mask)
    .then(replace_series)
    .otherwise(pl.col('group'))
    .alias('group')
)
print(result_df)

方法二:通过索引直接更新

先提取group==3的行索引,直接对这些位置赋值:

# 获取需要替换的行索引
target_indices = df.filter(pl.col('group') == 3).index
# 克隆原DataFrame避免修改原数据
result_df = df.clone()
# 直接更新对应索引位置的值
result_df[target_indices, 'group'] = values
print(result_df)

两种方法都能得到预期结果,方法一符合Polars链式调用的风格,方法二则更直观易懂。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 09:45:36