Python Forum
Adding 2-column CSV together in base Python without using pandas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Adding 2-column CSV together in base Python without using pandas (/thread-21419.html)



Adding 2-column CSV together in base Python without using pandas - LeegeeRicky - Sep-29-2019

I have a CSV file with ID column (Username) and two numeric columns. In base Python I want to get the ID and the sum of Auto and Manual Score, then generate another CSV with the result.

Example input CSV:
Username Auto Score Manual Score
1234, 1,
1234, 1,
1234, 1,
1234, , 1.5
345, 1,
345, 1,
345, , 2
133, 1,
133, 1,
133, , 2.5

Here is my code:
import csv

with open("exam_for_2019.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    
    for row in reader:
        sum_row = int(row['Auto Score']) + int(row['Manual Score'])
        print(sum_row)
But I got the error as: ValueError: invalid literal for int() with base 10: '' .

How can I fix it to sum the result with one Username with one sum score each row?


RE: Adding 2-column CSV together in base Python without using pandas - ichabod801 - Sep-29-2019

You need to check each value before you sum it, and if it's empty, treat it as zero. Something like:

auto_score = int(row['Auto Score']) if row['Auto Score'].strip() else 0



RE: Adding 2-column CSV together in base Python without using pandas - LeegeeRicky - Sep-29-2019

(Sep-29-2019, 11:59 AM)ichabod801 Wrote: You need to check each value before you sum it, and if it's empty, treat it as zero. Something like:
 auto_score = int(row['Auto Score']) if row['Auto Score'].strip() else 0 

I do a bit changed on my code as you said, but I got error when I try to sum then
import csv
 
with open("exam_for_2019.csv") as csvfile:
    reader = csv.DictReader(csvfile)
     
    for row in reader:
        auto_score = float(row['Auto Score']) if row['Auto Score'].strip() else 0
        Manual_Score = float(row['Manual Score']) if row['Manual Score'].strip() else 0
        sum_row = float(auto_score) + float(Manual_Score)
        sums = sum(sum_row)
        print(sums)
And I get TypeError: 'float' object is not iterable

(Sep-29-2019, 11:59 AM)ichabod801 Wrote: You need to check each value before you sum it, and if it's empty, treat it as zero. Something like:

auto_score = int(row['Auto Score']) if row['Auto Score'].strip() else 0

And also is possible assign the total number with each user name?
Like
Username Total
1234, 4.5
345, 4
133, 4.5


RE: Adding 2-column CSV together in base Python without using pandas - ichabod801 - Sep-29-2019

A couple of things. On line 7 you have float(row['Auto Score']). So auto_score is a float. Therefore, there is no reason for float(auto_score) on line 9. You can just use auto_score.

On line 9 you add the two scores together. So there is no need to sum them on line 10. And that is why you are getting an error. The sum function is for a list of numbers, and you are passing it one number. But like I said, you don't need that.

To get the full output, you are going to want to build a list with append in your for loop. Here's an example:

sums = []
for x in range(5):
    sums.append(x + x)
However, you are going to want to append a tuple of the username and the total. Once you have that list, you can send it through csv.writer to output the data to a file.