pandas的安装可以参见前面的博文,http://blog.csdn.net/piaoxuezhong/article/details/54023951
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包
类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的 。Series 和 DataFrame 分别对应于一维的序列和二维的表结构。pandas 约定俗成的导入方法如下:
>>> from pandas import Series,DataFrame
>>> import pandas as pd
1.Series
Series 可以看做一个定长的有序字典。基本任意的一维数据都可以用来构造 Series 对象:>>> s=Series([1,2,'abc'])
>>> s
0 1
1 2
2 abc
dtype: object
Series 对象包含两个主要的属性:index 和 values,分别为上例中左右两列。因为传给构造器的是一个列表,所以 index 的值是从 0 起递增的整数,如果传入的是一个类字典的键值对结构,就会生成 index-value 对应的 Series;或者在初始化的时候以关键字参数显式指定一个 index 对象:
>>> s=Series(data=[1,2,'abc'],index=['no1','no2','no3'])
>>> s
no1 1
no2 2
no3 abc
dtype: object
>>> s.index
Index([no1, no2, no3], dtype=object)
>>> s.values
array([1, 2, abc], dtype=object)
Series 对象的元素会严格依照给出的 index 构建,如果 data 参数是有键值对的,那么只有 index 中含有的键会被使用;如果 data 中缺少响应的键,即使给出 NaN 值,这个键也会被添加。Series 的 index 和 values 的元素之间虽然存在对应关系,但这与字典的映射不同。index 和 values 实际仍为互相独立的 ndarray 数组。Series 这种使用键值对的数据结构最大的好处在于,Series 间进行算术运算时,index 会自动对齐。另外,Series 对象和它的 index 都含有一个 name 属性:
>>> s.name='series_s'
>>> s.index.name='s\' index'
>>> s
s' index
no1 1
no2 2
no3 abc
Name: series_s, dtype: object
2.DataFrame
DataFrame 是一个表格型的数据结构,它含有一组有序的列(类似于 index),每列可以是不同的值类型(不像 ndarray 只能有一个 dtype)。基本上可以把 DataFrame 看成是共享同一个 index 的 Series 的集合。DataFrame 的构造方法与 Series 类似,只不过可以同时接受多条一维数据源,每一条都会成为单独的一列:
>>> data={'name':['fcq','jj','wym','xrr'],\
'year':[1989,1989,1989,1988],\
'm/f':['m','f','f','f']}
>>> data
{'m/f': ['m', 'f', 'f', 'f'], 'name': ['fcq', 'jj', 'wym', 'xrr'], 'year': [1989, 1989, 1989, 1988]}
>>> df=DataFrame(data)
>>> df
m/f name year
0 m fcq 1989
1 f jj 1989
2 f wym 1989
3 f xrr 1988
虽然参数 data 看起来是个字典,但字典的键并非充当 DataFrame 的 index 的角色,而是 Series 的 “name” 属性。这里生成的 index 仍是 “0123”。较完整的 DataFrame 构造器参数为:DataFrame(data=None,index=None,coloumns=None),columns 即 “name”:
>>> df=DataFrame(data,index=['no1','no2','no3','no4'])
>>> df
m/f name year
no1 m fcq 1989
no2 f jj 1989
no3 f wym 1989
no4 f xrr 1988
>>> df.columns
Index([m/f, name, year], dtype=object)
>>> df.index
Index([no1, no2, no3, no4], dtype=object)
3.对象属性
3.1查找索引
查找某个值在数组中的索引,类似于 Python 内建的 list.index(value) 方法。可以通过布尔索引来实现。比如我们想在一个 Series 中寻找到 ‘f’:>>> from pandas import Series,DataFrame
>>> import pandas as pd
>>> s=Series(list('I am fcq~'))
>>> s
0 I
1
2 a
3 m
4
5 f
6 c
7 q
8 ~
dtype: object
>>> s[s='f']
SyntaxError: invalid syntax
>>> s[s=='f']
5 f
dtype: object
3.2重新索引
Series 对象的重新索引通过reindex(index=None,**kwargs) 方法实现。**kwargs 中常用的参数有:method=None,fill_value=np.NaN:
>>> s2=Series([88,99,89,90],index=['ENG','CHE','PHY','CPT'])
>>> a=['ENG','CPT','CHE','PHY','MATH']
>>> s2.reindex(a)
ENG 88
CPT 90
CHE 99
PHY 89
MATH NaN
dtype: float64
>>> s2.reindex(a,fill_value=0)
ENG 88
CPT 90
CHE 99
PHY 89
MATH 0
dtype: int64
3.3排序和排名
Series 的 sort_index(ascending=True) 方法可以对 index 进行排序操作,ascending 参数用于控制升序或降序,默认为升序。若要按值对 Series 进行排序,当使用 .order(na_last=True, ascending=True, kind='mergesort') 方法,任何缺失值默认都会被放到 Series 的末尾。在 DataFrame 上,.sort_index(axis=0, by=None, ascending=True) 方法多了一个轴向的选择参数与一个 by 参数,by 参数的作用是针对某一(些)列进行排序(不能对行使用 by 参数):
>>> s2.sort_index(ascending=True)
CHE 99
CPT 90
ENG 88
PHY 89
dtype: int64
排名(Series.rank(method='average', ascending=True))的作用与排序的不同之处在于,他会把对象的 values 替换成名次(从 1 到 n)。这时唯一的问题在于如何处理平级项,方法里的 method 参数就是起这个作用的,他有四个值可选:average, min, max, first。>>> s3=Series([3,2,0,3],index=list('abcd'))
>>> s3
a 3
b 2
c 0
d 3
dtype: int64
>>> s3.rank()
a 3.5
b 2.0
c 1.0
d 3.5
dtype: float64
>>> s3.rank(method='max')
a 4
b 2
c 1
d 4
dtype: float64
>>> s3.rank(method='min')
a 3
b 2
c 1
d 3
dtype: float64
>>> s3.rank(method='first')
a 3
b 2
c 1
d 4
dtype: float64
3.4统计方法
>>> data = {'state':['Ohino','Ohino','Ohino','Nevada','Nevada'],
'year':[2000,2001,2002,2001,2002],
'pop':[1.5,1.7,3.6,2.4,2.9]}
>>> df=DataFrame(data)
>>> df
pop state year
0 1.5 Ohino 2000
1 1.7 Ohino 2001
2 3.6 Ohino 2002
3 2.4 Nevada 2001
4 2.9 Nevada 2002
>>> df.mean()
pop 2.42
year 2001.20
dtype: float64
>>> df.mean(axis=1)
0 1000.75
1 1001.35
2 1002.80
3 1001.70
4 1002.45
dtype: float64
其他常用的统计方法有: | |
count | 非 NA 值的数量 |
describe | 针对 Series 或 DF 的列计算汇总统计 |
min , max | 最小值和最大值 |
argmin , argmax | 最小值和最大值的索引位置(整数) |
idxmin , idxmax | 最小值和最大值的索引值 |
quantile | 样本分位数(0 到 1) |
sum | 求和 |
mean | 均值 |
median | 中位数 |
mad | 根据均值计算平均绝对离差 |
var | 方差 |
std | 标准差 |
skew | 样本值的偏度(三阶矩) |
kurt | 样本值的峰度(四阶矩) |
cumsum | 样本值的累计和 |
cummin , cummax | 样本值的累计最大值和累计最小值 |
cumprod | 样本值的累计积 |
diff | 计算一阶差分(对时间序列很有用) |
pct_change | 计算百分数变化 |
参考:
http://www.open-open.com/lib/view/open1402477162868.html
https://my.oschina.net/lionets/blog/277847
http://www.cnblogs.com/skying555/p/5914391.html
http://www.cnblogs.com/skying555/p/5914391.html