|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
任务4.读取 Excel 文件“bike_atemp_user_cnt.xlsx”,统计列 atemp 的最大值 maxValue、最小值 minValue、平均值 meanValue。利用 category =
[minValue, 0.4, 0.6,0.8,maxValue]和 labels = ['Cold', 'Cool', 'Warm', 'Hot']将 atemp 进行离散化;并将离散化结果作为一个新的列 Label 添加到原始数据 集,并保存为“bike_atemp_user_cnt_result.csv”。
我的代码如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def dataDescribeVisualization():
while True:
#读取数据
fn = input('请输入文件名: ')
try:
df_mean=pd.read_excel(fn,encoding='cp936')
df_mean_describe=df_mean.describe()
print(type(df_mean_describe))#<class'pandas.core.frame.DataFrame'>
print(df_mean_describe)
maxValue=df_mean_describe.at['max','atemp']
minValue=df_mean_describe.at['min','atemp']
meanValue=df_mean_describe.at['mean','atemp']
#将 atemp 进行离散化
category=[minValue,0.4,0.6,0.8,maxValue]
labels=['Cold','Cool','Warm','Hot']
#利用cut函数
df['Label']=pd.cut(df['atemp'],category,labels=labels)
try:
df.to_csv('bike_atemp_user_cnt_result.csv',index=False)
print('任务四完成')
break
except:
print('文件导出失败')
except:
print('文件名错误,请重试: ')
dataDescribeVisualization()
输入文件名后:
显示文件名错误,请重试:
请输入文件名:
|
|