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 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
Step: 1
To make your function more generic and accept a column number as a parameter to calculate the mean ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started