Python Forum
To extract a specific column from csv file and compute the average
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To extract a specific column from csv file and compute the average
#1
Hi all, I am using python 3.7 and encountering a difficulty in extract a column from a csv excel file. The excel file looks like this:

name, mon, tues, wed, thurs, fri

bob,120,130,124,125,177

ann,119,128,245,90,77

sam,200,78,220,234,168

what I need is to compute average spending on each day monday to friday. what I have now is:

import csv
    with open('data.csv', newline = '') as f:
        reader = csv.reader(f)
        
        next(reader)
        
        r1 = [float(row[1]) for row in reader]
        avg1 = round(sum(r1) / len(r1),2)
        
        r2 = [float(row[2]) for row in reader]
        avg2 = round(sum(r2) / len(r2),2)
        
        r3 = [float(row[3]) for row in reader]
        avg3 = round(sum(r3) / len(r3),2)
        
        r4 = [float(row[4]) for row in reader]
        avg4 = round(sum(r4) / len(r4),2)
        
        r5 = [float(row[5]) for row in reader]
        avg5 = round(sum(r5) / len(r5),2)
 
        print("================================================")
        print("{0:^10}|{1:^10}|{2:^10}|{3:^10}".format(avg1,avg2,avg3,avg4,avg5))


however, I've gotten this error:
" avg2 = round(sum(r2) / len(r2),2)

ZeroDivisionError: division by zero"

I try to print r2,r3,r4,r5 and found out that they are empty lists. but r1 and average1 is working. I am stucked here. Could anyone help??

At the mean time, may I get some hint on how can I find the person who spends the most on each day and print it out(print both if they spend the same)?
Reply
#2
Reader is an iterator. Generally, iterators are single use items. Once you go through them, you can't go through them again. So you go through it with the definition of r1, and then there's nothing left in for the definition of r2.

It would be better (more efficient) to write one for loop to go through the lines of the csv file, building lists for the individual days. If you've learned the zip function you could read it all in at once and transpose it.

If you build a list of names at the same time you are building a lists for each day, you can find the index of a maximum for the day in that day's list, and use that index to get the name of the person who spent it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for the hint. I will try your method!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Maths and python: Compute stress level cheerful 1 2,707 Oct-20-2021, 10:05 AM
Last Post: Larz60+
  Using regex to count specific character in file shamishd 1 1,608 Oct-01-2021, 07:33 AM
Last Post: snippsat
  [split] how to read a specific row in CSV file ? laxmipython 2 8,856 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,902 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Compute complex solutions in quadratic equations liam 1 1,880 Feb-09-2020, 04:18 PM
Last Post: Gribouillis
  How to compute conditional unigram probabilities? jbond 2 2,512 Jan-25-2020, 02:58 PM
Last Post: jbond
  CSV file column swapping DJPunk 5 6,283 Mar-04-2019, 04:11 AM
Last Post: DJPunk
  Write a program to compute the sum of the terms of the series: 4 - 8 + 12 - 16 + 20 - chewey777 0 2,804 Mar-24-2018, 12:39 AM
Last Post: chewey777
  Finding the average of numbers in a txt file piday 1 19,191 Feb-27-2018, 04:00 AM
Last Post: Larz60+
  How do you compute tf-idf from a list without using the counter class syntaxkiller 8 5,194 Dec-01-2017, 05:24 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020