0

I am quite new to Python and therefore this might seem easy but I am really stuck here.

I have a CSV file with values in a [525599 x 74] matrix. For each column of the 74 columns I would like to have the total sum of all 525599 values saved in one list.

I could not figure out the right way to iterate over each column and save the sum of each column in a list.

2 Answers2

1

Why don't you :

  • create a columnTotal integer array (one index for each column).
  • read the file line by line, per line:
    • Split the line using the comma as separator
    • Convert the splitted string parts to integers
    • Add the value of each column to the columnTotal array's colum index.
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

Since you're new to python I won't use any fancy libraries like pandas or numpy. But you should definitely check those out some time

import csv

reader = csv.reader(open('your_csv.csv', 'r'))
sums = [0] * 74
for row in reader:
    for i, element in enumerate(row):
        sums[i] += int(element)
print(sums)
Fred
  • 1,462
  • 8
  • 15
  • this just doesn't work. csv file contains strings, you have to convert to float / int first. And hardcoding the list at start is very bad practice. – Jean-François Fabre Dec 18 '18 at 11:14
  • Yeah I missed the conversion. Just edited it. Now... Do we really care about good convention when the guy is just learning Python? I just wanted to make it simple for the guy. If I cared about it I would probably just use pandas – Fred Dec 18 '18 at 11:19