1

I want to find the average of three columns in a csv file created from Python. My data is laid out like so:

[User Name, Score1, Score2, Score3]

for example

['James', 5, 8, 9]

I would like to find the average of the scores, 5, 8, 9.

newrecord = "{user_name},{score_1},{score_2},{score_3}\n".format(user_name=userName, score_1=quiz_scores[0], score_2=quiz_scores[1], score_3=quiz_scores[2])
file=open('classroom1.csv', "a+")
file.write(newrecord)
file.close()
with open('classroom1.csv') as csvfile:
    readCSV = csv.reader(csvfile)

I don't know what to do after this. Thanks in advance for any feedback.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Bob Stanley
  • 49
  • 3
  • 11

2 Answers2

2

Your readCSV object, when iterated over, will give you lists with strings, 4 values per row. Convert all but the first column to integers, then do your calculations on those integers:

for row in readCSV:
    name = row[0]
    scores = [int(c) for c in row[1:]]
    average = sum(scores) / len(scores)
    print('{}: {:.1f}'.format(name, average))

If you are using Python 2, then the / operator can cause problems as it'll use integer division when both the sum and the length are integers (which is the case here). Convert your numbers to float instead, or use 0.0 as the starting value for the sum (which makes sure the sum is a floating point number instead):

average = sum(scores, 0.0) / len(scores)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

I propose a numpy solution.

Consider the mockup file testfile.txt with the content

James, 5, 8, 9
Jeff, 10, 7, 3
Alice, 6, 7, 1

We can use numpy.loadtxtto load your file, then simply map the average to each row.

>>> import numpy as np
>>> map(np.mean, np.loadtxt('testfile.txt', usecols=[1,2,3], delimiter=','))
[7.333333333333333, 6.666666666666667, 4.666666666666667]
timgeb
  • 76,762
  • 20
  • 123
  • 145