Python for Data Analysis (8)

字典

#update方法,一个字典可以被合并到另一个字典中去:
d1={1:'a',2:'b'}
d1.update({3:'c',4:'d'})
d1
dict(zip(range(4),reversed(range(5))))
d1.get(1)

默认值!!

1.

一个常见的逻辑:

if key in some_dict:
value=some_dict[key]
else:
value=default_value

#可以简化成
value=some_dict.get(key,default_value)

2.

words={'apple','bat','bar','atom','book','momo'}
by_letter={}
for word in words:
    letter=word[0]
    if letter not in by_letter:
        by_letter[letter]=[word]
    else:
        by_letter[letter].append(word)
by_letter
{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book'], 'm': ['momo']}
#利用setdefault方法,让上面的if-else块可以写成:
by_letter.setdefault(letter,[]).append(word)
#还有简化方法collection模块中的一个defaultdict的类
from collections import defaultdict
by_letter=defaultdict(list)
for word in words:
    by_letter[word[0]].append(word)
by_letter
defaultdict(list,
            {'a': ['apple', 'atom'],
             'b': ['bat', 'bar', 'book'],
             'm': ['momo']})

defaultdict的初始化器只需要一个可调用对象(例如各种函数),并不需要明确的类型。因此,如果要将默认值设置为4,只需要传入一个能够返回4的函数即可:

counts=defaultdict(lambda:4)
counts
defaultdict(<function __main__.<lambda>>, {})

字典的哈希性

虽然字典的值是可以是任何Python对象,但键必须是不可变对象,这里描述为可哈希性,可以用hash函数来验证

hash('string')
-9167918882415130555
hash([13,3])#列表是可以变的,如果一定要用列表,就转变它为元组
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-20-d7b46a358c61> in <module>()
----> 1 hash([13,3])#列表是可以变的,如果一定要用列表,就转变它为元组


TypeError: unhashable type: 'list'
这本书主要是用 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、付费专栏及课程。

余额充值