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

如何在Pandas中用可变窗口长度实现rolling.min(无循环)

Pandas 基于可变窗口长度实现rolling.min(非循环方式)

我找过几个类似问题,但都解决不了我的需求。具体需求如下:

  • 在Pandas的DataFrame中,基于另一列的可变窗口长度使用rolling.min()方法
  • 数据集未来会扩大,希望不通过循环实现

尝试过的代码及报错

  1. 直接传入Series作为窗口参数:
pvdata['templow'] = pvdata['low'].rolling(window = pvdata['counter']).min()

报错信息:

ValueError: window must be an integer 0 or greater

我知道原因是rolling的window参数不能传Series,必须是整数。

  1. 使用apply+lambda尝试:
pvdata['templow'] = pvdata['counter'].apply(lambda x: pvdata['low'].rolling(window = x).min())

报错信息:

ValueError: Expected a 1D array, got an array with shape (10989, 10989)

这个错误我搞不懂。

期望输出

期望每行的templow值,是当前行往前数counter列指定的行数范围内,low列的最小值(示例为每行对应动态窗口最小值的表格)。

请问有没有办法不通过循环实现这个需求?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 01:59:54