Skip to content Skip to sidebar Skip to footer

Row Average Csv Python

Im looking for a piece of code that will print the average for each users score from a csv. It needs to read all scores and then work out an average across the row for each users.

Solution 1:

You can use the csv library to read the file. It is then just a case of calculating the averages:

import csv

withopen('example.csv') as handle:
    reader = csv.reader(handle)
    next(reader, None)
    for row in reader:
        user, *scores = row
        average = sum([int(score) for score in scores]) / len(scores)
        print (
            "{user} has average of {average}".format(user=user, average=average)
        )

With your input this prints:

elliott has average of 8.666666666666666
bob has average of 4.0
test has average of 0.5

This code requires python 3.

Post a Comment for "Row Average Csv Python"