我目前有一个csv文件,如下所示。第一部分仅显示列名称。
"f","p","g"
"foo","in","void"
"foo","out","void"
"foo","length","void"
...
g列的值对于每个f值都是相同的。唯一独特的部分是p。使用python,我如何将其组合如下:
"foo","in","out","length","void"
需要注意的一点是csv文件要大得多,一些f值可能有更多的p值。例如,它可能是这样的:
"goo","a","int"
"goo","b","int"
"goo","c","int"
"goo","d","int"
"goo","e","int"
"goo","f","int"
...
我希望我能正确理解你的问题。您可以按";f"g";列,然后聚合行:
x = df.groupby(["f", "g"], as_index=False)["p"].agg(list)
for vals in x.apply(lambda x: [x["f"], *x["p"], x["g"]], axis=1):
print(vals)
打印:
['foo', 'in', 'out', 'length', 'void']
['goo', 'a', 'b', 'c', 'd', 'e', 'f', 'int']