gpt4 book ai didi

python - 如何将CSV中的多行合并为一行

转载 作者:太空狗 更新时间:2023-10-30 02:44:01 26 4
gpt4 key购买 nike

我收到了一个大型 CSV 文件,我需要对其进行拆分以用于机器学习。我设法找到了一种将文件拆分为我需要的 2 行的方法 - 但我遇到了问题。

我基本上有这样一个文件结构。

 "David", "Red"
"David", "Ford"
"David", "Blue"
"David", "Aspergers"
"Steve", "Red"
"Steve", "Vauxhall"

我要求数据看起来更像这样......

"David, "Red", "Ford", "Blue", "Aspergers"
"Steve", "Red", "Vaxhaull"

我目前有这个来剥离 CSV 文件

import csv

cr = csv.reader(open("traits.csv","rb"), delimiter=',', lineterminator='\n')
cr.next() #skipping header line, no point in removing it as I need to standardise data manipuation.


# Print out the id of species and trait values
print 'Stripping input'
vals = [(row[1], row[4]) for row in cr]
print str(vals) + '\n'

with open("output.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(vals)
print 'Sucessfully written to file output.csv'


#for row in cr:
#print row

最佳答案

使用字典将名称存储为键,并将列表中的其他属性存储为值:

my_dict={}
with open("traits.csv","rb") as f:
cr = csv.reader(f, delimiter=',', lineterminator='\n')
for row in cr:
my_dict.setdefault(row[0].strip('" '),[]).append(row[1].strip('" '))

结果:

print my_dict
{'Steve': ['Red', 'Vauxhall'], 'David': ['Red', 'Ford', 'Blue', 'Aspergers']}

并写入新文件:

with open("output.csv", "wb") as f:
writer = csv.writer(f,delimiter=',')
for i,j in my_dict.iteritems():
writer.writerow([i]+j)

setdefault(key[, default])

If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.

关于python - 如何将CSV中的多行合并为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31229480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com