How to get an average from a row then make a list out of it [duplicate]

2024/11/18 21:26:14

If I have a csv data that gives two row values of:

years grades
2001  98
2001  75
2008  100
2003  57
2008  50

I have more values but I will try to explain what I am trying to get here.

I want to get the average for each year. For instance, for 2001, the answer would be (98+75)/(# of counts 2001, which is 2 in this case).

 def construct_values(filing):"""Parameters----------Returns-------years: array of integersaverage_grades: array of floats
"""years, average_grades = [], []grades = []with open('grades.csv', 'r') as filing:next(filing)for row in file_path:year, grade = (s.strip() for s in row.split(','))years.append(year)grades.append(grade)return years, average_grades

What I did was just to get two arrays of years and grades. I don't know how to get average arrays and then print out like:

2001, 88.5555 for instance if 88.555 is average.

Instead of being them as dictionaries, what I want to have are two arrays that will just return together.

Answer

Why not build a dictionary of grades with the key being each year:

from collections import defaultdict
grades = defaultdict(lambda: [])with open('grades.csv', 'r') as f:year, grade = (s.strip() for s in row.split(','))grades[year].append(grade)

Then print the averages:

for y, g in grades:print('{}: {}', y, sum(g) / float(len(g)))
https://en.xdnf.cn/q/118622.html

Related Q&A

Beautiful soup: Extract everything between two tags when these tags have different ids

Beautiful soup: Extract everything between two tags I have seen a question through the above link where we are getting the information between two tags. Whereas I need to get the information between th…

exceptions.RuntimeError - Object has no attribute errno [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How can I translate this python function to c++?

I am trying to translate a python function to c++ without success. Can someone help me? The python function receives as input a string S and 2 integers (fragment_size and jump). The aim of this functi…

Reverse PDF imposition

I have an imposed document: there are 4 n A4 pages on the n sheets. I put them into a roller image scanner and receive one 2 n paged PDF document (A3).If, say, n = 3, then Ive got the following seque…

Python: How to run flask mysqldb on Windows machine?

Ive installed the flask-mysqldb module with pip package management system on my Windows machine and I dont know how to run it.I have tried to add the path to the MySQLdb in System properties and still …

Match a pattern and save to variable using python

I have an output file containing thousands of lines of information. Every so often I find in the output file information of the following formInput Orientation: ... content ... Distance matrix (angstro…

Sharing a Queue instance between different modules

I am new to Python and I would like to create what is a global static variable, my thread-safe and process-safe queue, between threads/processes created in different modules. I read from the doc that t…

Square a number with functions in python [duplicate]

This question already has answers here:What does it mean when the parentheses are omitted from a function or method call?(6 answers)Closed last year.This is an extremely easy question for Python. Its…

Changing the cell name

I have a file that contains the following:NameABCD0145ABCD1445ABCD0998And Im trying to write a cod that read every row and change the name to the following format:NameABCD_145ABCD_1445ABCD_998keeping i…

Procfile Heroku

I tried to deploy my first Telegram chatbot (done with Chatterbot library) on Heroku. The files of my chatbot are: requirements (txt file) Procfile (worker: python magghybot.py) botusers (csv file) Mag…