Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum each column in csv

Tags:

python

csv

Given a csv file

A,0,0,1,0
B,0,0,1,0
C,0,0,1,0
D,0,0,1,0
E,0,0,1,0
F,0,0,0,1

I'd like to compute the totals for each column. Is there a more pythonic or efficient way to do this than:

import csv

totals = [0]*4

for row in csv.reader(csvfile):
    counts = [ int(x) for x in row[-4:] ]
    totals = [ sum(x) for x in zip(counts, totals) ]
print(totals)
like image 825
RaGe Avatar asked Jun 04 '26 08:06

RaGe


1 Answers

transpose the csv file beforehand, skip the now title column and just compute sum on each row

cr = zip(*csv.reader(csvfile))
next(cr)

result = [sum(map(int,x)) for x in cr]
print(result)

[0, 0, 5, 1]

careful as it loads the whole file in memory when expanding the arguments for zip, though.

like image 98
Jean-François Fabre Avatar answered Jun 05 '26 23:06

Jean-François Fabre