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

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.