Pandas--“大熊猫”基础
Series
Series: pandas的长枪(数据表中的一列或一行,观测向量,一维数组...)
Series1 = pd.Series(np.random.randn(4))
print Series1,type(Series1)
print Series1.index
print Series1.values
输出结果:
0 -0.676256
1 0.533014
2 -0.935212
3 -0.940822
dtype: float64 <class 'pandas.core.series.Series'>
Int64Index([0, 1, 2, 3], dtype='int64')
[-0.67625578 0.53301431 -0.93521212 -0.94082195]
- np.random.randn() 正态分布相关。函数说明
Series⽀持过滤的原理就如同NumPy
print Series1>0
print Series1[Series1>0]
输出结果如下:
0 0.030480
1 0.072746
2 -0.186607
3 -1.412244
dtype: float64 <class 'pandas.core.series.Series'>
Int64Index([0, 1, 2, 3], dtype='int64')
[ 0.03048042 0.07274621 -0.18660749 -1.41224432]
我发现,逻辑表达式,获得的值就是True或者False。要先取得值,还是要X[y]的形式。
当然也支持广播Broadcasting
什么是broadcasting
,暂时我也不太清楚,看个栗子:
print Series1*2
print Series1+5
输出结果如下:
0 0.06096
1 1 0.145492
2 -0.373215
3 -2.824489
dtype: float64
0 5.030480
1 5.072746
2 4.813393
3 3.587756
dtype: float64
以及Universal Function
numpy.frompyfunc(out,nin,nout) 返回的是一个函数,nin是输入的参数个数,nout是函数返回的对象的个数函数说明
在序列上就使用行标,而不是创建1个2列的数据表,能够轻松辨别哪是数据,哪是元数据
这句话的意思,我的理解是序列尽量是一列,不用去创建2列,这样子,使用index就能指定数据了
Series2 = pd.Series(Series1.values,index=['norm_'+unicode(i) for i in xrange(4)])
print Series2,type(Series2)
print Series2.index
print type(Series2.index)
print Series2.values
输出结果如下,可以看到,它是通过修改了index
值的样式,并没有创建2列。
norm_0 -0.676256
norm_1 0.533014
norm_2 -0.935212
norm_3 -0.940822
dtype: float64 <class 'pandas.core.series.Series'>
Index([u'norm_0', u'norm_1', u'norm_2', u'norm_3'], dtype='object')
<class 'pandas.core.index.Index'>
[-0.67625578 0.53301431 -0.93521212 -0.94082195]
虽然行是有顺序的,但是仍然能够通过行级的index来访问到数据:
(当然也不尽然像Ordered Dict,因为⾏索引甚⾄可以重复,不推荐重复的行索引不代表不能用)
print Series2[['norm_0','norm_3']]
可以看到,读取数据时,确实要采用X[y]的格式。这里X[[y]]是因为,它要读取两个数据,指定的是这两个数据的index
值,将index
值存放进list
中,然后读取。输出结果如下:
norm_0 -0.676256
norm_3 -0.940822
dtype: float64
再比如:
print 'norm_0' in Series2
print 'norm_6' in Series2
输出结果:
True
False
逻辑表达式的输出结果,布尔型值。
从Key不重复的Ordered Dict或者从Dict来定义Series就不需要担心行索引重复:
Series3_Dict = {"Japan":"Tokyo","S.Korea":"Seoul","China":"Beijing"}
Series3_pdSeries = pd.Series(Series3_Dict)
print Series3_pdSeries
print Series3_pdSeries.values
print Series3_pdSeries.index
输出结果:
China Beijing
Japan Tokyo
S.Korea Seoul
dtype: object
['Beijing' 'Tokyo' 'Seoul']
Index([u'China', u'Japan', u'S.Korea'], dtype='object')
通过上面的输出结果就知道了,输出结果是无序的,和输入顺序无关。
想让序列按你的排序⽅式保存?就算有缺失值都毫无问题
Series4_IndexList = ["Japan","China","Singapore","S.Korea"]
Series4_pdSeries = pd.Series( Series3_Dict ,index = Series4_IndexList)
print Series4_pdSeries
print Series4_pdSeries.values
print Series4_pdSeries.index
print Series4_pdSeries.isnull()
print Series4_pdSeries.notnull()
上面这样的输出就会按照list
中定义的顺序输出结果。
整个序列级别的元数据信息:name
当数据序列以及index本身有了名字,就可以更方便的进行后续的数据关联啦!
这里我感觉就是列名的作用。下面举例:
print Series4_pdSeries.name
print Series4_pdSeries.index.name
很显然,输出的结果都是None
,因为我们还没指定name
嘛!
Series4_pdSeries.name = "Capital Series"
Series4_pdSeries.index.name = "Nation"
print Series4_pdSeries
输出结果:
Nation
Japan Tokyo
China Beijing
Singapore NaN
S.Korea Seoul
Name: Capital Series, dtype: object
"字典"?不是的,⾏index可以重复,尽管不推荐。
Series5_IndexList = ['A','B','B','C']
Series5 = pd.Series(Series1.values,index = Series5_IndexList)
print Series5
print Series5[['B','A']]
输出结果:
A 0.030480
B 0.072746
B -0.186607
C -1.412244
dtype: float64
B 0.072746
B -0.186607
A 0.030480
dtype: float64
我们可以看出,Series['B']输出了两个值,所以index值尽量不要重复呀!
DataFrame
DataFrame:pandas的战锤(数据表,⼆维数组)
Series的有序集合,就像R的DataFrame一样方便。
仔细想想,绝大部分的数据形式都可以表现为DataFrame。
从NumPy二维数组、从文件或者从数据库定义:数据虽好,勿忘列名
dataNumPy = np.asarray([('Japan','Tokyo',4000),('S.Korea','Seoul',1300),('China','Beijing',9100)])
DF1 = pd.DataFrame(dataNumPy,columns=['nation','capital','GDP'])
DF1