Python for Data Analysis (5)

本文介绍了Python中异常处理的基本用法,包括try/except、try/except/finally和try/except/else/finally等结构,并通过实例展示了如何处理常见的ValueError和TypeError。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常处理

  1. try/except
  2. try/except someerror
  3. try/finally
  4. try/except/else/fianlly
#1.try/except,想编写一个出错时能返回输入参数的一个float函数
def attempt_float(x):
    try:
        return float(x)
    except:  #except后不加任何指定错误,则如果出现任何错误后,都会执行except后的语句
        return x
attempt_float('some')
'some'
#2.try/except +someerror,后面指定的可以是一个错误,也可以是多个错误
def attempt_float(x):
    try:
        return float(x)
    except ValueError:
        return x
attempt_float((1,2))
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-11-a1f7e5239136> in <module>()
----> 1 attempt_float((1,2))


<ipython-input-8-392841f26717> in attempt_float(x)
      2 def attempt_float(x):
      3     try:
----> 4         return float(x)
      5     except ValueError:
      6         return x


TypeError: float() argument must be a string or a number
#于是出现了ValueError之外的错误则添加一个:
def attempt_float(x):
    try:
        return float(x)
    except (ValueError,TypeError):
        return x
attempt_float((1,2))
(1, 2)
#3. try/except finally
#有时你可能不想处理任何异常,而只是希望有一段代码不管try块代码成功于否都能被执行。
f =open(path,'w')
try:
    write_to_file(f)
finally:
    f.close()
#4. try/except/else/fianlly
#想让代码只在try块成功时执行,使用else即可:
f=open(path,'w')
try:
    write_to_file(f)
except:
    print 'FAILED'
else:
    print 'Succeeded'
finally:
    f.close()
这本书主要是用 pandas 连接 SciPy 和 NumPy,用pandas做数据处理是Pycon2012上一个很热门的话题。另一个功能强大的东西是Sage,它将很多开源的软件集成到统一的 Python 接口。, Python for Data Analysis is concerned with the nuts and bolts of manipulating, processing, cleaning, and crunching data in Python. It is also a practical, modern introduction to scientific computing in Python, tailored for data-intensive applications. This is a book about the parts of the Python language and libraries you’ll need to effectively solve a broad set of data analysis problems. This book is not an exposition on analytical methods using Python as the implementation language., Written by Wes McKinney, the main author of the pandas library, this hands-on book is packed with practical cases studies. It’s ideal for analysts new to Python and for Python programmers new to scientific computing., Use the IPython interactive shell as your primary development environment, Learn basic and advanced NumPy (Numerical Python) features, Get started with data analysis tools in the pandas library, Use high-performance tools to load, clean, transform, merge, and reshape data, Create scatter plots and static or interactive visualizations with matplotlib, Apply the pandas groupby facility to slice, dice, and summarize datasets, Measure data by points in time, whether it’s specific instances, fixed periods, or intervals, Learn how to solve problems in web analytics, social sciences, finance, and economics, through detailed examples
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值