Oct-19-2018, 02:22 PM
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:
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)?
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)?