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

如何用Python按CVE ID去重并合并去重后的Product Versions列

基于CVE ID去重并合并去重后的Product Versions列

原始数据

CVE ID             Product Versions
0      CVE-2022-46689                   Mac OS 12
1      CVE-2022-42856                      Safari
2      CVE-2022-46689             Windows 10 21h1
3      CVE-2022-41121             Windows 10 21h2
4      CVE-2022-42856                      Safari

目标结果

CVE ID             Product Versions
0      CVE-2022-46689            Mac OS 12, Windows 10 21h1
1      CVE-2022-42856            Safari
2      CVE-2022-41121            Windows 10 21h2

实现代码

使用Pandas的分组与自定义聚合操作即可完成需求:

import pandas as pd

# 构造原始DataFrame(已有数据可跳过此步)
df = pd.DataFrame({
    'CVE ID': ['CVE-2022-46689', 'CVE-2022-42856', 'CVE-2022-46689', 'CVE-2022-41121', 'CVE-2022-42856'],
    'Product Versions': ['Mac OS 12', 'Safari', 'Windows 10 21h1', 'Windows 10 21h2', 'Safari']
})

# 核心操作:分组后去重合并
result_df = df.groupby('CVE ID')['Product Versions'].apply(
    lambda x: ', '.join(pd.unique(x))
).reset_index()

# 重置索引为连续整数(可选)
result_df.index = range(len(result_df))

print(result_df)

关键步骤说明

  • groupby('CVE ID'):将数据按CVE ID分组,相同CVE的行归为一组
  • pd.unique(x):对每组内的Product Versions值去重,保留唯一条目
  • ', '.join(...):把去重后的版本值用「逗号+空格」连接成字符串
  • reset_index():将分组结果转换为标准DataFrame结构,恢复列名与连续索引

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 17:20:41