1.导入数据import numpy as npimport csv2.获取数据iris_data=[]with open("iris.csv") as csvfile: csv_reader=csv.reader(csvfile) #用csv.reader读取csvfile文件 birth_header=next(csv_reader) #读取第一行各列 for row in csv_reader: iris_data.append(row)3.数据清理,去掉索引号iris_list=[]for row in iris_data: iris_list.append(tuple(row[1:]))iris_list4.数据统计(1)创建数据类型datatype=np.dtype([("Sepal.Length",np.str_,40), ("Sepal.Width",np.str_,40), ("Petal.Length",np.str_,40), ("Petal.Width",np.str_,40), ("Species",np.str_,40)])datatype(2)创建二维数组iris_data=np.array(iris_list,dtype=datatype)iris_data(3)将待处理数据的类型转化为float类型PetalLength=iris_data["Petal.Length"].astype(float)PetalLength(4)排序np.sort(PetalLength)(5)数据去重np.unique(PetalLength)(6)对指定列求和、均值、标准差、方差、最小值及最大值print("sum:",np.sum(PetalLength))print("mean:",np.mean(PetalLength))print("std:",np.std(PetalLength))print("var:",np.var(PetalLength))print("min:",np.min(PetalLength))print("max:",np.max(PetalLength))
运行结果如下:
来源地址:https://blog.csdn.net/m0_56107252/article/details/127324725