0

I have a set of data describing each people's spending in an excel file with their names as first 2 columns and spending on each day within a week as the next few columns. Such as:

name, mon, tues, wed, thurs, fri

bob,120,130,124,125,177

ann,119,128,245,90,77

sam,200,78,220,234,168

Now I want to find out :

  1. the average spending of them each day
  2. the person who spends the max each day

I've gotten this code so far:

import csv
with open('spending.csv', newline = '') as f:
    reader = csv.reader(f)

    next(reader)

    r1 = [float(row[3]) for row in reader]
    avg1 = round(sum(r1) / len(r1),2)

    r2 = [float(row[4]) for row in reader]
    avg2 = round(sum(r2) / len(r2),2)

    r3 = [float(row[5]) for row in reader]
    avg3 = round(sum(r3) / len(r3),2)

    r4 = [float(row[6]) for row in reader]
    avg4 = round(sum(r4) / len(r4),2)

    r5 = [float(row[7]) for row in reader]
    avg5 = round(sum(r5) / len(r5),2)

    print("================================================")
    print("{0:^10}|{1:^10}|{2:^10}|{3:^10}|{4:^10}".format(avg1,avg2,avg3,avg4,avg5))

However, I got error like this:

avg2 = round(sum(r2) / len(r2),2)

ZeroDivisionError: division by zero

Please help me with this, and how can I find out the person spends the most each day? Is there a function max()? Appreciate your help!

zzzTeee
  • 53
  • 7

1 Answers1

0

For the first question about to read a specific column, you can try Split() function : Reading specific columns from a text file in python

For the second one, about average : pandas get column average/mean

Hope this can help you

Mohamad TAGHLOBI
  • 581
  • 5
  • 11
  • Ah sorry! Can you use Statistics module? If yes, this is a way to compute average : https://www.geeksforgeeks.org/python-statistics-mean-function/amp/ – Mohamad TAGHLOBI Oct 18 '18 at 05:19
  • I can find the average for first column, but python just print empty lists for the next few columns... that's why this error appears – zzzTeee Oct 19 '18 at 14:24