然后计算并将结果插入到python中的csv中

时间:2013-09-22 21:21:07

标签: python csv python-2.7 dataset sum

这是我的第一篇文章,但我希望你能告诉我如何执行计算并在csv数据文件中插入值。

对于每一行,我希望能够获取每个'uniqueclass'并对第12列中获得的分数求和。参见下面的示例数据;

text1,Data,Class,Uniqueclass1,data1,data,2,data2,data3,data4,data5,175,12,data6,data7
text1,Data,Class,Uniqueclass1,data1,data,2,data2,data3,data4,data5,171,18,data6,data7
text1,Data,Class,Uniqueclass2,data1,data,4,data2,data3,data4,data5,164,5,data6,data7
text1,Data,Class,Uniqueclass2,data1,data,4,data2,data3,data4,data5,121,21.5,data6,data7
text2,Data,Class,Uniqueclass2,data1,data,4,data2,data3,data4,data5,100,29,data6,data7
text2,Data,Class,Uniqueclass2,data1,data,4,data2,data3,data4,data5,85,21.5,data6,data7
text3,Data,Class,Uniqueclass3,data1,data,3,data2,data3,data4,data5,987,35,data6,data7
text3,Data,Class,Uniqueclass3,data1,data,3,data2,data3,data4,data5,286,18,data6,data7
text3,Data,Class,Uniqueclass3,data1,data,3,data2,data3,data4,data5,003,5,data6,data7

因此,例如第一个Uniqueclass持续前两行。因此,我希望能够在该行上插入一个后续值,即'346'(175和17的总和)。结果将如下所示:

text1,Data,Class,Uniqueclass1,data1,data,2,data2,data3,data4,data5,175,12,data6,data7,346
text1,Data,Class,Uniqueclass1,data1,data,2,data2,data3,data4,data5,171,18,data6,data7,346

我希望能够为每个独特类'

执行此操作

感谢SMNALLY

1 个答案:

答案 0 :(得分:3)

对于这类事情,我总是喜欢defaultdict类。

这是我的尝试:

from collections import defaultdict

class_col = 3
data_col = 11

# Read in the data
with open('path/to/your/file.csv', 'r') as f:
    # if you have a header on the file
    # header = f.readline().strip().split(',')
    data = [line.strip().split(',') for line in f]

# Sum the data for each unique class.
# assuming integers, replace int with float if needed
count = defaultdict(int)
for row in data:
    count[row[class_col]] += int(row[data_col])

# Append the relevant sum to the end of each row
for row in xrange(len(data)):
    data[row].append(str(count[data[row][class_col]]))

# Write the results to a new csv file
with open('path/to/your/new_file.csv', 'w') as nf:
    nf.write('\n'.join(','.join(row) for row in data))