鱼C论坛

 找回密码
 立即注册
查看: 1650|回复: 7

求大佬看一下问题在哪里

[复制链接]
发表于 2020-6-27 17:40:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()

输入文件名后:

  显示文件名错误,请重试:
                           请输入文件名:


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-27 17:48:11 | 显示全部楼层
[b]把 最外面的  try-except 删了 看看报错在第几行呀[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-27 17:52:00 | 显示全部楼层
Twilight6 发表于 2020-6-27 17:48
把 最外面的  try-except 删了 看看报错在第几行呀

这一行df_mean=pd.read_excel(fn,encoding='cp936')
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-27 17:53:36 | 显示全部楼层
开始吧我 发表于 2020-6-27 17:52
这一行df_mean=pd.read_excel(fn,encoding='cp936')


报错内容看看~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-27 17:54:10 | 显示全部楼层

IndentationError: unexpected indent
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-27 17:58:51 | 显示全部楼层
开始吧我 发表于 2020-6-27 17:54
IndentationError: unexpected indent

....这个是缩进问题?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-27 18:00:22 | 显示全部楼层
Twilight6 发表于 2020-6-27 17:58
....这个是缩进问题?

大佬,我真的发现不了错误呀,┭┮﹏┭┮
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-27 18:25:56 | 显示全部楼层
开始吧我 发表于 2020-6-27 18:00
大佬,我真的发现不了错误呀,┭┮﹏┭┮



我也不懂了,你重新复制运行看看:

  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt


  4. def dataDescribeVisualization():
  5.     while True:
  6.         # 读取数据
  7.         fn = input('请输入文件名: ')
  8.         try:
  9.             df_mean = pd.read_excel(fn, encoding='cp936')
  10.             df_mean_describe = df_mean.describe()
  11.             print(type(df_mean_describe))  # <class'pandas.core.frame.DataFrame'>
  12.             print(df_mean_describe)
  13.             maxValue = df_mean_describe.at['max', 'atemp']
  14.             minValue = df_mean_describe.at['min', 'atemp']
  15.             meanValue = df_mean_describe.at['mean', 'atemp']
  16.             # 将 atemp 进行离散化
  17.             category = [minValue, 0.4, 0.6, 0.8, maxValue]
  18.             labels = ['Cold', 'Cool', 'Warm', 'Hot']
  19.             # 利用cut函数
  20.             df['Label'] = pd.cut(df['atemp'], category, labels=labels)
  21.             try:
  22.                 df.to_csv('bike_atemp_user_cnt_result.csv', index=False)
  23.                 print('任务四完成')
  24.                 break
  25.             except:
  26.                 print('文件导出失败')
  27.         except:
  28.             print('文件名错误,请重试: ')


  29. dataDescribeVisualization()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 16:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表