Find the Standard Deviation from a CSV File using Python -
i have csv file named 'salaries.csv' content of files follows:
city,job,salary delhi,doctors,500 delhi,lawyers,400 delhi,plumbers,100 london,doctors,800 london,lawyers,700 london,plumbers,300 tokyo,doctors,900 tokyo,lawyers,800 tokyo,plumbers,400 lawyers,doctors,300 lawyers,lawyers,400 lawyers,plumbers,500 hong kong,doctors,1800 hong kong,lawyers,1100 hong kong,plumbers,1000 moscow,doctors,300 moscow,lawyers,200 moscow,plumbers,100 berlin,doctors,800 berlin,plumbers,900 paris,doctors,900 paris,lawyers,800 paris,plumbers,500 paris,dog catchers,400
i need print standard deviation of salaries of each profession.
older version of python. cannot use statistics , numpy.
from __future__ import with_statement import math import csv open("salaries.csv") f: def average(f): return sum(f) * 1.0 / len(f) variance = map(lambda x: (x - avg)**2, f) standard_deviation = math.sqrt(average(variance)) print standard_deviation
can me, newbie field of python.
error : typeerror('argument 2 map() must support iteration',)
the output should
plumbers 311 lawyers 286 doctors 448
to details per profession, create dictionary instead:
from __future__ import with_statement import math def get_stats(profession, salaries): n = float(len(salaries)) mean = sum(salaries)/n stdev = 0 value in salaries: stdev += (value - mean)**2 stdev = math.sqrt(stdev/(n)) print profession, min(salaries), max(salaries), mean, stdev open('salaries.csv') f: f.readline() # create list of salaries salaries = {} line in f.readlines(): country, profession, value = line.split(',') value = int(value.strip()) profession = profession.strip() if salaries.has_key(profession): salaries[profession].append(value) else: salaries[profession] = [value] k,v in salaries.items(): get_stats(k,v)
Comments
Post a Comment