《python葡萄酒品质分析》由会员分享,可在线阅读,更多相关《python葡萄酒品质分析(3页珍藏版)》请在金锄头文库上搜索。
1、python葡萄酒品质分析实验的1.掌握数据读取的函数2.熟悉for语句循环遍历3.掌握列表、元组、字典和集合的特效以及常法(append().len()4.灵活掌握通过切式获取数据实验内容(代码)#1.读取数据path=r./white_wine.csvimport csvif _name_ = _main_: f = open(path) reader = csv.reader(f)#delimiter字段分割符,默认是逗号,每个单元格作为个元素,112个元素 # reader=csv.reader(f,delimiter=;) content = for row in reader: c
2、ontent.append(row) f.close() for i in range(5): print(contenti,len(contenti)#2.处理数据#(1)查看葡萄酒共分为种等级。quality_list = for row in content1: quality_list.append(int(row-1)quality_count = set(quality_list)print(f葡萄酒共有len(quality_count)种等级,分别是quality_count)#Python3.6新增的f-字符串格式化#(2)葡萄酒等级有7种,统计在每个品质中的样本量conte
3、nt_dict = #品质:样本量,for row in content1: quality = int(row-1) if quality not in content_dict.keys(): content_dictquality = row#把row当做只有的维数据加字典作为键当前quality的值 #print(row) #print(row) else: content_dictquality.append(row) #当前键quality对应的值为个维数据集,row作为位数据的追加到维数据的末尾for key in content_dict: print(fkey等级:len(c
4、ontent_dictkey)#print(content_dict9)#(3)统计在每个品质的样本量number_tuple = for key,value in content_dict.items(): number_tuple.append(key,len(value)print(number_tuple)#(4)求每个数据集中“fixed acidity”的均值。mean_list = for key,value in content_dict.items(): sum = 0 for row in value: sum += float(row0) # 第1列数据 mean_list.append(key,sum/len(value)#元组和列表都样for item in mean_list: print(fitem0:item1)运结果: