1、# python编程基础,主要包括python的基本语法,内建数据结构、函数以及文件操作
# 1、读取数据 import csv f = open('./xiaoqu_data/1xiaoqu_shijiazhuang2.csv', 'r', encoding='UTF-8') reader = csv.reader(f) data = [] for row in reader: data.append(row) # 显示data前5行 for i in range(5): print(data[i]) # 2、处理数据 # 1)查看小区中共有几种关注数据 guanzhu_list = [] for row in data: # 小区关注等级数据 guanzhu_list.append(int(row[2])) guanzhu_count = set(guanzhu_list) print(" 小区关注共有%d种数据,分别是:%r" %(len(guanzhu_count),guanzhu_count)) # 2)按小区关注等级将数据集划分为7个子集,并统计每种等级的数量 content_dict = {} for row in data: guanzhu = int(row[2]) if guanzhu not in content_dict.keys(): # 用字典保存每个子集 content_dict[guanzhu] = [row] else: content_dict[guanzhu].append(row) for key in content_dict: print(" ",key,':',len(content_dict[key])) # 3)计算每个数据集中 元/平方 的均值 mean_list = [] for key,value in content_dict.items(): sum = 0 for row in value: # 元/平方 是第四列数据 sum = sum + float(row[3]) mean_list.append((key, sum/len(value))) # 打印均值 for item in mean_list: print(item[0],":",item[1]) f.close()