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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With