python在线考试第九章测试题目

鸢尾花数据处理

【问题描述】

现要处理鸢尾花数据iris.csv,
1.保留sepal_length大于5的数据;
2.按照鸢尾花的种类(species)分组,得出每种鸢尾花的sepal_width最大值;
3.将四列(sepal_length,sepal_width,petal_length,petal_width)属性相加的结果形成新的一列fertilizer(化肥),表示要施加化肥的数量;
4.按照鸢尾花的种类(species)分组,得出每种鸢尾花的fertilizer平均值;
5.将2与4得到的结果形成DataFrame,按照fertilizer平均值排序后(由小到大)将最终结果写入iris_result.csv文件。

【输入形式】

文件,iris.csv

使用pd.read_csv(‘iris.csv’)读取

【输出形式】

使用to_csv将DataFrame保存

xxx.to_csv(‘iris_result.csv’,index=False,header=True)

【样例输入】

输入的文件内容如下:

sepal_length,sepal_width,petal_length,petal_width,species
4.8,3.4,1.9,0.2,setosa
4.8,3.1,1.6,0.2,setosa
6.8,2.8,4.8,1.4,versicolor
4.9,3.6,1.4,0.1,setosa
6.1,3.0,4.9,1.8,virginica
5.5,2.3,4.0,1.3,versicolor
5.7,2.6,3.5,1.0,versicolor
5.6,2.9,3.6,1.3,versicolor
5.9,3.0,4.2,1.5,versicolor
4.6,3.2,1.4,0.2,setosa
6.7,3.3,5.7,2.1,virginica
5.5,4.2,1.4,0.2,setosa
7.2,3.2,6.0,1.8,virginica
6.6,3.0,4.4,1.4,versicolor
6.2,2.8,4.8,1.8,virginica
6.7,3.3,5.7,2.5,virginica
5.5,2.4,3.7,1.0,versicolor
5.0,3.4,1.5,0.2,setosa
4.9,3.1,1.5,0.1,setosa
6.0,2.2,4.0,1.0,versicolor
6.0,2.2,5.0,1.5,virginica
5.7,2.8,4.5,1.3,versicolor
5.1,3.8,1.9,0.4,setosa
5.0,3.5,1.3,0.3,setosa
5.7,4.4,1.5,0.4,setosa
6.1,2.6,5.6,1.4,virginica
5.2,3.4,1.4,0.2,setosa
6.7,2.5,5.8,1.8,virginica
7.9,3.8,6.4,2.0,virginica
6.3,3.4,5.6,2.4,virginica
5.8,2.7,5.1,1.9,virginica
6.3,3.3,6.0,2.5,virginica
7.2,3.6,6.1,2.5,virginica
4.8,3.0,1.4,0.3,setosa
4.7,3.2,1.3,0.2,setosa
5.1,3.3,1.7,0.5,setosa
6.2,3.4,5.4,2.3,virginica
6.5,3.0,5.8,2.2,virginica
7.0,3.2,4.7,1.4,versicolor
4.3,3.0,1.1,0.1,setosa
5.8,4.0,1.2,0.2,setosa
6.0,2.7,5.1,1.6,versicolor
6.4,2.7,5.3,1.9,virginica
5.7,2.9,4.2,1.3,versicolor
4.7,3.2,1.6,0.2,setosa
6.2,2.2,4.5,1.5,versicolor
5.0,3.0,1.6,0.2,setosa
4.4,3.2,1.3,0.2,setosa
6.3,2.5,5.0,1.9,virginica
7.7,3.0,6.1,2.3,virginica
6.4,2.8,5.6,2.2,virginica
4.9,3.0,1.4,0.2,setosa
5.8,2.7,5.1,1.9,virginica
5.3,3.7,1.5,0.2,setosa
5.5,2.4,3.8,1.1,versicolor
6.9,3.1,5.4,2.1,virginica
5.4,3.7,1.5,0.2,setosa
6.6,2.9,4.6,1.3,versicolor
5.1,3.8,1.5,0.3,setosa
5.8,2.6,4.0,1.2,versicolor
6.1,2.9,4.7,1.4,versicolor
6.3,2.9,5.6,1.8,virginica
6.5,2.8,4.6,1.5,versicolor
5.0,3.5,1.6,0.6,setosa
6.0,3.0,4.8,1.8,virginica
5.4,3.9,1.7,0.4,setosa
4.8,3.0,1.4,0.1,setosa
5.7,2.8,4.1,1.3,versicolor
6.8,3.0,5.5,2.1,virginica
6.5,3.0,5.5,1.8,virginica
5.0,2.0,3.5,1.0,versicolor
5.1,3.4,1.5,0.2,setosa
5.9,3.0,5.1,1.8,virginica
5.0,2.3,3.3,1.0,versicolor
7.2,3.0,5.8,1.6,virginica

【样例输出】

对应的输出文件内容为:

sepal_width,fertilizer
4.4,10.936363636363637
3.2,14.29473684210526
3.8,17.011111111111113

import pandas as pd

df=pd.read_csv('iris.csv')
df=df[df.sepal_length>5]
max1=df.groupby('species')['sepal_width'].max()
df['fertilizer']=df['sepal_length']+df['sepal_width']+df['petal_length']+df['petal_width']
mean=df.groupby('species')['fertilizer'].mean()
res=pd.DataFrame({
     'sepal_width':max1,'fertilizer':mean})
res=res.sort_values(by='fertilizer') 
res.to_csv('iris_result.csv',index=False,header=True)                       

你可能感兴趣的:(python)