How to use Python to analyse cvs file? -
i beginner of programming, trying work out how average of elements in csv file. content of csv file:
and code:
import csv suburbs_average = {'north':0,'south':0,'east':0,'west':0} suburbs_count = {'north':0,'south':0,'east':0,'west':0} csvfile = open("ps1_3_data.csv") csv_reader = csv.reader(csvfile, delimiter=',') row in csv_reader: print (row[0],float(row[1])) print(suburbs_average)
i have spent whole day trying figure out how calculate average of each suburbs(east,north...). need sort same suburb before calculate , how? please me on this? in advance. cheers.
i working standard library think should stick can understand base implementations before installing modules willy-nilly.
the csv
module part of standard library, , reading docs learn each module capable of when looking solution. given problem, i'd following:
iterate through
csv.reader()
object , put valuesdict()
s, average them.
import csv # keep track of each time see 1 count = {'north': 0, 'south': 0, 'east': 0, 'west': 0} # store associated values each time see 1 avg = {'north': [], 'south': [], 'east': [], 'west': []} # rid of newline if not py3 open("ps1_3_data.csv", 'r', newline='') f: r = csv.reader(f, delimiter=',') # assumes dont have header row # if do, uncomment line below # header = next(r) row in r: # increment count each time see 1 count[row[0]] += 1 # add suburbs list of values avg[row[0]].append(float(row[1]) # here replace lists single value # goes {'north': [15.0, 10.0, 10.0]} # {'north': 11.6666...} suburb in avg: avg[suburb] = (sum(avg[suburb])/len(avg[suburb])
i made csv data supplied test:
>>> import csv >>> temp = r'c:\users\paul\desktop\temp.csv' >>> count = {'north': 0, 'south': 0, 'east': 0, 'west': 0} >>> avg = {'north': [], 'south': [], 'east': [], 'west': []} >>> open(temp, 'r', newline = '') f: r = csv.reader(f, delimiter=',') row in r: count[row[0]] += 1 avg[row[0]].append(float(row[1])) >>> count {'north': 3, 'south': 4, 'east': 5, 'west': 5} >>> avg {'north': [15.0, 10.0, 10.0], 'south': [10.0, 5.0, 5.0, 10.0], 'east': [10.0, 5.0, 10.0, 5.0, 10.0], 'west': [20.0, 15.0, 15.0, 20.0, 5.0]} >>> suburb in avg: avg[suburb] = (sum(avg[suburb])/len(avg[suburb])) >>> avg {'north': 11.666666666666666, 'south': 7.5, 'east': 8.0, 'west': 15.0} >>>
Comments
Post a Comment