简体   繁体   English

在Python中将多个CSV行合并为1

[英]Merge multiple CSV rows into 1 in Python

I have a CSV file with hundreds of rows like shown below!我有一个包含数百行的 CSV 文件,如下所示! How can I merge these two rows in such a way that only second column value gets updated to CVE-2017-3006, CVE-2017-3007 and the rest remains the same我怎样才能合并这两行,使得只有第二列值更新为CVE-2017-3006、CVE-2017-3007 ,其余保持不变

在此处输入图片说明

import pandas as pd

# Create a dummy dataframe like yours
df = pd.DataFrame([
    {"C1": "0", "C2": "A", "C3": "9.0", "C4": "High", "C5": "zaid", "C6": "TCP", "C7": "445", "C8": "some_text", "C9": "some_other_text"},
    {"C1": "0", "C2": "B", "C3": "9.0", "C4": "High", "C5": "zaid", "C6": "TCP", "C7": "445", "C8": "some_text", "C9": "some_other_text"},
    {"C1": "1", "C2": "A", "C3": "17.0", "C4": "High", "C5": "zaid", "C6": "TCP", "C7": "445", "C8": "some_text", "C9": "some_other_text"},
    {"C1": "1", "C2": "B", "C3": "17.0", "C4": "High", "C5": "zaid", "C6": "TCP", "C7": "445", "C8": "some_text", "C9": "some_other_text"},
    {"C1": "1", "C2": "C", "C3": "17.0", "C4": "High", "C5": "zaid", "C6": "TCP", "C7": "445", "C8": "some_text", "C9": "some_other_text"},
])

# Group by all columns you want to keep, and aggregate C2 into a list
grouped = df.groupby(["C1","C3","C4","C5","C6","C7","C8","C9"], as_index=False).agg({
    "C2":lambda x:x.tolist()
})
print(grouped)

      C1    C3    C4    C5   C6   C7         C8               C9         C2
0  0   9.0  High  zaid  TCP  445  some_text  some_other_text     [A, B]
1  1  17.0  High  zaid  TCP  445  some_text  some_other_text  [A, B, C]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM