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

Pandas双列累计求和任意阈值触发同步重置及分组编号问题

实现思路

你之前的错误核心是分开计算两个列的累计和,二者重置逻辑相互独立没有绑定,只要把两个累计和的计算、阈值判断、重置逻辑放在同一个遍历流程中处理即可,任意一列触发阈值就同步重置两个累计值,同时store编号自增。

正确实现代码
import pandas as pd

# 构造测试数据集
data = {
    'item': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
    'price': [10, 20, 5, 6, 7, 8, 20, 6],
    'units': [1, 5, 8, 3, 4, 2, 2, 3]
}
df = pd.DataFrame(data)

threshold_price = 31
threshold_units = 8

# 初始化状态变量
cur_cum_price = 0
cur_cum_units = 0
cur_store = 0
cs_price_res = []
cs_units_res = []
store_res = []

for _, row in df.iterrows():
    next_price = cur_cum_price + row['price']
    next_units = cur_cum_units + row['units']
    # 任意阈值超限就触发同步重置
    if next_price > threshold_price or next_units > threshold_units:
        cur_cum_price = row['price']
        cur_cum_units = row['units']
        cur_store += 1
    else:
        cur_cum_price = next_price
        cur_cum_units = next_units
    # 存入结果
    cs_price_res.append(cur_cum_price)
    cs_units_res.append(cur_cum_units)
    store_res.append(cur_store)

# 写入DataFrame
df['cs_price'] = cs_price_res
df['cs_units'] = cs_units_res
df['store'] = store_res
输出结果
itempriceunitscs_pricecs_unitsstore
A1011010
B2053060
C58581
D63632
E741372
F82823
G2022843
H63634

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 17:09:01