You need to enable JavaScript to run this app. 如何为Pandas Dataframe添加部件完整性与产品最新版本标识列
优惠活动
大模型
产品
解决方案
定价
更多

如何为Pandas Dataframe添加部件完整性与产品最新版本标识列

实现方案

完整代码

import pandas as pd

# 以下是样例数据构造,实际使用时替换为你自己的DataFrame读取逻辑即可
data = [
    ["Product1", 1, 3, 0],
    ["Product1", 2, 3, 0],
    ["Product2", 1, 1, 0],
    ["Product2", 1, 1, 1],
    ["Product3", 1, 2, 0],
    ["Product3", 2, 2, 0],
]
df = pd.DataFrame(data, columns=["product", "part_id", "total_parts", "version"])

# 计算complete标识列
df['complete'] = df.groupby(['product', 'version'])['part_id']\
                   .transform('nunique')\
                   .eq(df['total_parts'])\
                   .map({True: 'Y', False: 'N'})

# 计算is_the_latest标识列
df['is_the_latest'] = df.groupby('product')['version']\
                        .transform('max')\
                        .eq(df['version'])\
                        .map({True: 'Y', False: 'N'})

# 输出结果验证
print(df)

逻辑说明

  • complete列:以product和version为分组维度,统计每个分组下不重复的部件编号数量,和该分组的总部件数对比,相等说明所有部件都已收录,标记为Y,否则标记为N。
  • is_the_latest列:以product为分组维度,取每个产品的最大版本号,当前行的版本号等于最大值则标记为Y,否则标记为N。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 04:36:07