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

按条件递增行值并按组保留最新值的实现需求

按分组生成递增/继承的new_val字段问题

需求:

  • 按no字段分组,每组首行的new_val设为1
  • 当cond=False时,new_val在上一行new_val基础上加1
  • 当cond=True时,new_val继承上一行的new_val

输入数据:

nocondval
0001/1True1
0001/1False1
0001/1True1
0001/1False1
0001/2False1
0001/2True1
0001/2False1
0001/2False1
0001/2True1
0001/3True1
0001/3False1
0001/3True1

预期输出:

nocondvalnew_val
0001/1True11
0001/1False12
0001/1True12
0001/1False13
0001/2False11
0001/2True11
0001/2False12
0001/2False13
0001/2True13
0001/3True11
0001/3False12
0001/3True12

解决方案

方法1:向量化实现(高效)

利用分组标记和累计求和,避免循环,适合大数据量:

import pandas as pd

# 构造输入数据(实际使用时替换为你的数据读取逻辑)
data = {
    'no': ['0001/1']*4 + ['0001/2']*5 + ['0001/3']*3,
    'cond': [True, False, True, False, False, True, False, False, True, True, False, True],
    'val': [1]*12
}
df = pd.DataFrame(data)

# 1. 将cond=False转为1,True转为0
df['flag'] = (~df['cond']).astype(int)
# 2. 分组后把每组首行的flag设为0
df['flag'] = df.groupby('no')['flag'].transform(lambda x: x.mask(x.index == x.index[0], 0))
# 3. 分组累计求和后加1,得到new_val
df['new_val'] = df.groupby('no')['flag'].cumsum() + 1
# 4. 可选:删除中间flag列
df = df.drop('flag', axis=1)

print(df)

方法2:自定义分组函数(直观)

逐行计算逻辑,更易理解:

import pandas as pd

# 构造输入数据
data = {
    'no': ['0001/1']*4 + ['0001/2']*5 + ['0001/3']*3,
    'cond': [True, False, True, False, False, True, False, False, True, True, False, True],
    'val': [1]*12
}
df = pd.DataFrame(data)

def compute_new_val(group):
    new_vals = [1]  # 首行固定为1
    for cond in group['cond'][1:]:
        if not cond:
            new_vals.append(new_vals[-1] + 1)
        else:
            new_vals.append(new_vals[-1])
    group['new_val'] = new_vals
    return group

df = df.groupby('no', group_keys=False).apply(compute_new_val)
print(df)

两种方法均能得到符合预期的结果,根据数据规模选择即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 13:20:32