一、Series
一组数据及与之相关的数据索引组成
(1)创建
列表创建
b = pd.Series([9, 8, 7, 6], index=['a', 'b', 'c', 'd'])
标量值创建
b = pd.Series(25, index=['a', 'b', 'c', 'd'])
字典类型
b = pd.Series({'a':9,'b':8,'c':7})
ndarray类型
m = pd.Series(np.arange(5),index=np.arange(9,4,-1))
(2)基本操作
(3)对齐操作
(4)name属性
二、DataFrame
共用相同索引的一组列组成
常用于表达二维数据,但可以表达多维数据
(1)创建
二维ndarray对象创建
一维ndarray对象字典创建
列表类型创建
(2)数据类型操作
重新索引
import numpy as np
import pandas as pd
d1 = {'city':['Beijing', 'Shanghai', 'Guanzhou', 'Shenzhen', 'Shenyang'],
'MoM':[101.5, 101.2, 101.3, 102.6, 100.1],
'YoY':[120.7, 127.3, 119.4, 140.9, 101.4],
'base':[121.4, 127.8, 120.6, 145.5, 101.6]}
d = pd.DataFrame(d1, index=['c1', 'c2', 'c3', 'c4', 'c5'])
d = d.reindex(columns=['city', 'MoM', 'YoY', 'base'])
print d
print d.index
city MoM YoY base
c1 Beijing 101.5 120.7 121.4
c2 Shanghai 101.2 127.3 127.8
c3 Guanzhou 101.3 119.4 120.6
c4 Shenzhen 102.6 140.9 145.5
c5 Shenyang 100.1 101.4 101.6
Index类型
Index([u'c1', u'c2', u'c3', u'c4', u'c5'], dtype='object')
reindex(index=None, columns=None,...)的参数
index, columns:新的行列自定义索引
fill_value:填充缺失位置的值
method:ffill向前填充,bfill 向后填充
limit:最大填充量
new = d.columns.insert(4, 'new')
newd = d.reindex(columns=new, fill_value=200)
print newd
city MoM YoY base new
c1 Beijing 101.5 120.7 121.4 200
c2 Shanghai 101.2 127.3 127.8 200
c3 Guanzhou 101.3 119.4 120.6 200
c4 Shenzhen 102.6 140.9 145.5 200
c5 Shenyang 100.1 101.4 101.6 200
删除
d = d.drop('c5')
print d
d = d.drop('YoY',axis=1)
print d
city MoM YoY base
c1 Beijing 101.5 120.7 121.4
c2 Shanghai 101.2 127.3 127.8
c3 Guanzhou 101.3 119.4 120.6
c4 Shenzhen 102.6 140.9 145.5
city MoM base
c1 Beijing 101.5 121.4
c2 Shanghai 101.2 127.8
c3 Guanzhou 101.3 120.6
c4 Shenzhen 102.6 145.5
(3)算术运算
a = pd.DataFrame(np.arange(12).reshape(3,4))
print a
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
b = pd.DataFrame(np.arange(20).reshape(4,5))
print b
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
print a + b
0 1 2 3 4
0 0.0 2.0 4.0 6.0 NaN
1 9.0 11.0 13.0 15.0 NaN
2 18.0 20.0 22.0 24.0 NaN
3 NaN NaN NaN NaN NaN
print a * b
0 1 2 3 4
0 0.0 1.0 4.0 9.0 NaN
1 20.0 30.0 42.0 56.0 NaN
2 80.0 99.0 120.0 143.0 NaN
3 NaN NaN NaN NaN NaN
方法形式的运算
add、sub、mul、div
print b.add(a, fill_value = 100)
0 1 2 3 4
0 0.0 2.0 4.0 6.0 104.0
1 9.0 11.0 13.0 15.0 109.0
2 18.0 20.0 22.0 24.0 114.0
3 115.0 116.0 117.0 118.0 119.0
print a.mul(b, fill_value = 0)
0 1 2 3 4
0 0.0 1.0 4.0 9.0 0.0
1 20.0 30.0 42.0 56.0 0.0
2 80.0 99.0 120.0 143.0 0.0
3 0.0 0.0 0.0 0.0 0.0
c = pd.Series(np.arange(4))
print c - 10
0 -10
1 -9
2 -8
3 -7
print b - c
0 1 2 3 4
0 0.0 0.0 0.0 0.0 NaN
1 5.0 5.0 5.0 5.0 NaN
2 10.0 10.0 10.0 10.0 NaN
3 15.0 15.0 15.0 15.0 NaN
print b.sub(c, axis=0)
0 1 2 3 4
0 0 1 2 3 4
1 4 5 6 7 8
2 8 9 10 11 12
3 12 13 14 15 16
(4)比较运算
e = pd.DataFrame(np.arange(12, 0, -1).reshape(3,4))
print e
0 1 2 3
0 12 11 10 9
1 8 7 6 5
2 4 3 2 1
print a > e
0 1 2 3
0 False False False False
1 False False False True
2 True True True True
print a == e
` 0 1 2 3
0 False False False False
1 False False True False
2 False False False False