Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum a csv column in python

Tags:

python

csv

sum

I'm trying to make a sum of a column in a csv file. The file looks like:

Date  Value
2012-11-20  12
2012-11-21  10
2012-11-22  3

This can be in the range of hundreds of rows. I need to get the total of Value (in this case it would be 25) printed on to a terminal. I so far have some code but it's resulting in a much smaller figure than it should sum. When troubleshooting it, I did a print of the sum and realized that instead of summing 12 + 10 + 3, it actually breaks the numbers in each column and sums as 1 + 2 + 1 + 0 + 3, which obviously equals to a much smaller total. Here's my code, if someone could make a recommendation would be great!

with open("file.csv")) as fin:
  headerline = fin.next()
  total = 0
  for row in csv.reader(fin):
    print col # for troubleshooting
    for col in row[1]:
      total += int(col)
  print total
like image 297
janecs Avatar asked Nov 22 '12 16:11

janecs


People also ask

How do you sum a table in Python?

sum() function in Python Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.


1 Answers

The csv module loops over your rows one by one, there is no need to then loop over the column. Just sum int(row[1]):

with open("file.csv") as fin:
    headerline = fin.next()
    total = 0
    for row in csv.reader(fin):
        total += int(row[1])
    print total

You can use a shortcut with a generator expression and the sum() built-in function:

with open("file.csv") as fin:
    fin.next()
    total = sum(int(r[1]) for r in csv.reader(fin))

Note that in Python, strings are sequences too, so when you do for col in row[1]: you are looping over the individual characters of row[1]; so for your first row that'd be 1 and 2:

>>> for c in '123':
...     print repr(c)
...
'1'
'2'
'3'
like image 158
Martijn Pieters Avatar answered Oct 12 '22 13:10

Martijn Pieters