Question: Make the function more generic rather than hard coding the age column, have the function accept a second parameter the column number that the function

Make the function more generic rather than hard coding the age column, have the function accept a second parameter the column number that the function should take the average of. for example, if you want to calculate the mean of column #3 of a table called "mydata.csv". the parameters should will be mean_calc('mydata.csv', 3).
 
import csv
line = 0
tot = 0.000
with open ('birthwt(4).csv', 'r') as csv_file:
    birthwt = csv.reader (csv_file, delimiter = ',')
    for row in birthwt:
        if line !=0:
            print(row[2])
            tot = tot + float(row[2])
        line +=1
print(tot)
print(str(tot/line-1))


id 85 86 87 88 89 91 low 0 0 0 0 0 0 age 19 33 20 21 18 21 Iwt 182 155 105 108 107 124 race 2 3 1 3 smoke 0 0 

id 85 86 87 88 89 91 low 0 0 0 0 0 0 age 19 33 20 21 18 21 Iwt 182 155 105 108 107 124 race 2 3 1 3 smoke 0 0 1 1 1 O ptl 0 0 0 0 0 ht 0 0 0 0 0 ui 1 0 0 1 1 0 ftv 0 3 2 0 0 bwt 2523 2551 2557 2594 2600 2622

Step by Step Solution

3.49 Rating (152 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To make your function more generic and accept a column number as a parameter to calculate the mean ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!