python分析csv文件
发布网友
发布时间:2022-04-27 01:56
我来回答
共1个回答
热心网友
时间:2022-06-22 03:53
import csv
suburbs_average = {}
suburbs_count = {}
group_suburb = {}
csvfile = open("ps1_3_data.csv")
csv_reader = csv.reader(csvfile, delimiter=',')
for row in csv_reader:
suburbs=row[0]
travel_time=row[1]
if suburbs in group_suburb.keys():
suburbs_count[suburbs] += 1
group_suburb[suburbs] += int(travel_time)
else:
suburbs_count[suburbs] = 1
group_suburb[suburbs] = int(travel_time)
for key in group_suburb.keys():
suburbs_average[key]=group_suburb[key]/suburbs_count[key]
print (suburbs_average)