Python For Data Analysis -- Pandas

首先pandas的作者就是这本书的作者
对于Numpy,我们处理的对象是矩阵
pandas是基于numpy进行封装的,pandas的处理对象是二维表(tabular, spreadsheet-like),和矩阵的区别就是,二维表是有元数据的
用这些元数据作为index更方便,而Numpy只有整形的index,但本质是一样的,所以大部分操作是共通的

大家碰到最多的二维表应用,关系型数据库中的表,有列名和行号,这些就是元数据
当然你可以用抽象的矩阵来对这些二维表做统计,但使用pandas会更方便

 

Introduction to pandas Data Structures

Series

A Series is a one-dimensional array-like object containing an array of data (of any NumPy data type) and an associated array of data labels, called its index.
简单的理解,就是字典,或一维表;不显式指定index时,会自动添加 0 through N - 1的整数作为index

image

image

这里可以简单的替换index,生成新的series,

image

大家想想,对于Numpy而言,没有显式的指定index,但也是可以通过整形的index取到数据的,这里的index其实本质上和numpy的整形index是一样的
所以对于Numpy的操作,也同样适用于pandas

image

image

同时,上面说了series其实就是字典,所以也可以用python字典来初始化

image

 

DataFrame

A DataFrame represents a tabular, spreadsheet-like data structure containing an ordered collection of columns, each of which can be a different value type (numeric, string, boolean, etc.).

如果接触过R,应该对DataFrame很熟悉,其实pandas就从某种程度上模拟出R的一些功能
所以如果用python也可以像R一样方便的做统计,那何必要再去用R

上面Series是字典或一维表,
DataFrame是二维表,也可以看作是series的字典

image

image

指定了列名,行名是自动生成的

同时也可以指定行名,这里增加了debt列,但是没有数据,所以是NaN

image

可以为debt,赋值

image

取行,用ix

image

也可以用嵌套字典来创建Dataframe,其实是series的字典,series本身就是字典,所以就是嵌套的字典

image

image

可以像numpy矩阵一样,转置

image

 

Essential Functionality

下面看看到底pandas在这些数据结构上提供了哪些方便的functions

Reindexing

A critical method on pandas objects is reindex, which means to create a new object with the data conformed to a new index.

其实就是更改indexing

image

增加e,并默认填上0

image

还可以通过method参数,来指定填充方式

image

可以选择向前或向后填充

image

对于二维表,可以在index和columns上同时进行reindex

image

image

image

reindex的参数,

image

 

Dropping entries from an axis

用axis指定维度,对于二维表,行是0,列是1

image

 

Indexing, selection, and filtering

基本和Numpy差不多

image

 

Arithmetic and data alignment

数据对齐和自动填充是pandas比较方便的一点

In [136]: df1 = DataFrame(np.arange(12.).reshape((3, 4)), columns=list('abcd'))
In [137]: df2 = DataFrame(np.arange(20.).reshape((4, 5)), columns=list('abcde'))

image

可以看到默认情况下,只有两个df都有的情况下,才会相加,否则为NaN
我觉得大部分情况,应该是希望有一个就加一个,即把没有的初始化为0

image

除了add,还支持

image

 

Function application and mapping

1. Element-wise:NumPy ufuncs (element-wise array methods) work fine with pandas objects:

image

另一种element-wise,使用applymap

image

 

2. 可以将func apply到每一行或每一列

image

比较复杂的case

image

image

 

3.对于某个行或列,即series进行map

image

 

Summarizing and Computing Descriptive Statistics

提供很多类似R的统计函数,

image

image

image

image

提供类似R中的descirbe,很方便

image

对非数值型,执行describe

image

汇总表,

image

 

Correlation and Covariance,相关系数和协方差

image

对MSFT和IBM之间求相关系数和协方差

image

也可以求出相关系数矩阵和协方差矩阵

image

 

Unique Values, Value Counts, and Membership

In [217]: obj = Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])

In [218]: uniques = obj.unique()
In [219]: uniques
Out[219]: array([c, a, d, b], dtype=object)

In [220]: obj.value_counts()
Out[220]:
c 3
a 3
b 2
d 1

image

 

Handling Missing Data

提供一些用于处理missing data的工具函数

image

其中fillna复杂些,

image

image

image

 

Hierarchical Indexing

Hierarchical indexing is an important feature of pandas enabling you to have multiple (two or more) index levels on an axis. Somewhat abstractly, it provides a way for you to work with higher dimensional data in a lower dimensional form.

可以使用多层分级的index,其实本质等同于增加一维,所以相当于用低维来模拟高维数据

image

image

image

并且是支持,通过unstack和stack来还原多维数据的

image

image

 

Pandas还提供其他功能,尤其是ETL功能,方便数据处理

比如和各种文件读入和写出的功能

cleaning, transform(基于map), merge(join)……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值