Row Average CSV Python

2024/11/23 0:37:16

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.

It also needs to calculate how many scores there are to accurately work out the average score so if there are only 2 tests completed it then needs divide by 2.

The CSV is

STUDENT,SCORE1,SCORE2,SCORE3  
elliott,12,2,12  
bob,0,11,1
test,0,1

I need the code to work out all users averages as described above in the CSV and then print the output.

Cheers.

Answer

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

import csvwith open('example.csv') as handle:reader = csv.reader(handle)next(reader, None)for row in reader:user, *scores = rowaverage = 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.

https://en.xdnf.cn/q/120668.html

Related Q&A

To input a file and get a formatted string output using .format()

Write a function named calculate_expenses that receives a filename as argument. The file contains the information about a persons expenses on items. Your function should return a list of tuples sorted …

How to use python regex to extract IP address from server log files?

I am currently getting started with python. I have a server log file for the pages I visited over a period of time. How do I write a python program to find out which IP address was visited most? Will …

DateFormatter returning wrong dates - Matplotlib/Pandas [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions concerning problems with code youve written must describe the specific problem — and incl…

What does the error IndentationError: expected an indented block in python mean? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How to insert character in csv cell in python?

Im new with python. Here is my csv file :data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; dataThe thing that I want…

How can you initialise an instance in a Kivy screen widget

I am trying to access an instance variable named self.localId in my kivy screen and it keeps saying the saying the instance doesnt exist after i have initialised it. I know I have an error In my code b…

is a mathematical operator classed as an interger in python

in python is a mathematical operator classed as an interger. for example why isnt this code workingimport randomscore = 0 randomnumberforq = (random.randint(1,10)) randomoperator = (random.randint(0,2)…

Fix a function returning duplicates over time?

I have a function here that returns a 4 digit string. The problem is that when I run the function like 500 times or more, it starts to return duplicates. How to avoid that?My Function:import random de…

Pandas method corr() use not all features

I have dataframe with shape (335539, 26). So I have 26 features. But when i use data.corr() I get a 12 x 12 matrix.What can be wrong? `

int to datetime in Python [duplicate]

This question already has answers here:Convert string "Jun 1 2005 1:33PM" into datetime(26 answers)Parsing datetime in Python..?(2 answers)Closed 5 years ago.Im receiving data from the port.…