Pandas是基于NumPy构建的,让以NumPy为中心的应用变得更加简单。Pandas的数据结构主要有两个:Series和DataFrame。
Series
Series是一种类似于一维数组的对象,它由数据和索引两部分构成,你可以把它理解成一种字典,同时它可以由字典创建而来。索引在左边,值在右边。如果没有指定索引,那么它的索引默认从0开始。
我们可以分别用index和values来输出索引和值。
import pandas as pd
obj = pd.Series([4,1,9,0,-5])
print(obj)
print(obj.index)
print(obj.values)
# 0 4
# 1 1
# 2 9
# 3 0
# 4 -5
# dtype: int64
# RangeIndex(start=0, stop=5, step=1)
# [ 4 1 9 0 -5]
同时,我们可以指定它的索引。
import pandas as pd
data = {'a':1,'b':2,'c':3,'d':4}
obj = pd.Series(data)
print(obj)
index = ['a1','b1','c1','d1']
obj = pd.Series(data=obj.values,index=index)
print(obj)
# a 1
# b 2
# c 3
# d 4
# dtype: int64
# a1 1
# b1 2
# c1 3
# d1 4
# dtype: int64
Series在算术运算中会自动对其不同索引的数据。对于不是两者同时存在的索引,值会用NaN补齐。
import pandas as pd
data1 = {'a':1,'b':2,'c':3,'d':4}
data1 = pd.Series(data1)
data2 = {'b':2,'c':3,'d':4,'e':5}
data2 = pd.Series(data2)
print(data1 + data2)
# a NaN
# b 4.0
# c 6.0
# d 8.0
# e NaN
# dtype: float64
DataFrame
DataFrame是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型。DataFrame既有行索引也有列索引,它可以被看做由Series组成的字典(共用同一个索引),它的数据是以一个或多个二维存放的。
data = {'state':['qwe','rty','uio','asd','fgh','jkl'],
'year':[2000,2001,2002,2003,2004,2005],
'pop':[1.2,3.4,5.6,7.8,9,10]}
frame = pd.DataFrame(data)
print(frame)
# state year pop
# 0 qwe 2000 1.2
# 1 rty 2001 3.4
# 2 uio 2002 5.6
# 3 asd 2003 7.8
# 4 fgh 2004 9.0
# 5 jkl 2005 10.0
DataFrame中我们可以利用索引字段来选取数据,索引字段有ix、iloc、loc三个,其中iloc通过选取行(列)位置编号索引数据;loc通过选取行(列)标签索引数据;ix是两者都可以,但是后面将不再支持。
注意,在iloc的行中,我们是[0:3],后面输出0、1、2,也就是左闭右开;loc和ix我们也是[0:3],我们的输出是0、1、2、3,也就是完全闭合。
data = {'state':['qwe','rty','uio','asd','fgh','jkl'],
'year':[2000,2001,2002,2003,2004,2005],
'pop':[1.2,3.4,5.6,7.8,9,10]}
frame = pd.DataFrame(data)
print(frame.iloc[0:3,0])
print(frame.loc[0:3,'state'])
print(frame.ix[0:3,0])
# 0 qwe
# 1 rty
# 2 uio
# Name: state, dtype: object
# 0 qwe
# 1 rty
# 2 uio
# 3 asd
# Name: state, dtype: object
# 0 qwe
# 1 rty
# 2 uio
# 3 asd
# Name: state, dtype: object
通过索引方式返回的列只是相应数据的视图而已,没有数据的复制。
注意,DataFrame的索引对象是不可修改的。
重新索引
pandas对象的 一个重要方法是reindex,其作用是创建一个适应新对象。调用Series的reindex将会根据新索引进行重排,当某个索引值当前不存在,就会引入缺失值。
在reindex的时候,也可以加入method,当method=ffill时,就是向前填充;当method=bfill时,就是向后填充。
reindex可以修改索引和列,其中index是修改索引,columns是修改列。
frame = pd.DataFrame(np.arange(9).reshape((3,3)),index=['a','c','d'],
columns=['ohio','texas','california'])
print(frame)
state = ['a','b','c']
print(frame.reindex(columns=state))
print(frame.reindex(index=['a','b','c','d']))
print(frame.reindex(index=['a','b','c','d'],method='ffill'))
print(frame.reindex(index=['a','b','c','d'],method='bfill'))
# ohio texas california
# a 0 1 2
# c 3 4 5
# d 6 7 8
# a b c
# a NaN NaN NaN
# c NaN NaN NaN
# d NaN NaN NaN
# ohio texas california
# a 0.0 1.0 2.0
# b NaN NaN NaN
# c 3.0 4.0 5.0
# d 6.0 7.0 8.0
# ohio texas california
# a 0 1 2
# b 0 1 2
# c 3 4 5
# d 6 7 8
# ohio texas california
# a 0 1 2
# b 3 4 5
# c 3 4 5
# d 6 7 8
处理缺失值
缺失数据在数据处理中非常常见,在数据缺失的时候,pandas都是用NaN来填充,我们也可以用isnull和notnull来查看数据集中是否有NaN值。
python内置的None值也会被当作NaN处理。
data = pd.Series(['asd','qwe',None,'poi',np.nan])
print(data)
print(data.isnull())
print(data.notnull())
# 0 asd
# 1 qwe
# 2 None
# 3 poi
# 4 NaN
# dtype: object
# 0 False
# 1 False
# 2 True
# 3 False
# 4 True
# dtype: bool
# 0 True
# 1 True
# 2 False
# 3 True
# 4 False
# dtype: bool
我们有dropna函数来滤除缺失数据,可以通过阈值来调节对缺失值的容忍度。可以通过how来确定滤除的范围,当how=all的时候,就是滤除行或列全为NaN的。还能用axis来指定是行还是列,当axis=0时,是按行;当axis=1时,是按列。
data = pd.DataFrame([[1,2,3],[4,np.nan,6],[None,8,9],[None,None,None]])
print(data)
print(data.dropna())
print(data.dropna(how='all'))
print(data.dropna(axis=1,how='all'))
# 0 1 2
# 0 1.0 2.0 3.0
# 1 4.0 NaN 6.0
# 2 NaN 8.0 9.0
# 3 NaN NaN NaN
# 0 1 2
# 0 1.0 2.0 3.0
# 0 1 2
# 0 1.0 2.0 3.0
# 1 4.0 NaN 6.0
# 2 NaN 8.0 9.0
# 0 1 2
# 0 1.0 2.0 3.0
# 1 4.0 NaN 6.0
# 2 NaN 8.0 9.0
# 3 NaN NaN NaN
我们利用fillna函数来填充缺失的值,前面我们还介绍过ffill和bfill方法。
data = pd.DataFrame([[1,2,3],[4,np.nan,6],[None,8,9],[None,None,None]])
print(data)
print(data.fillna(99))
print(data.fillna(data.mean()))
# 0 1 2
# 0 1.0 2.0 3.0
# 1 4.0 NaN 6.0
# 2 NaN 8.0 9.0
# 3 NaN NaN NaN
# 0 1 2
# 0 1.0 2.0 3.0
# 1 4.0 99.0 6.0
# 2 99.0 8.0 9.0
# 3 99.0 99.0 99.0
# 0 1 2
# 0 1.0 2.0 3.0
# 1 4.0 5.0 6.0
# 2 2.5 8.0 9.0
# 3 2.5 5.0 6.0
附上一篇pandas的一些知识总结
https://blog.csdn.net/fengchi863/article/details/80484314
《用python进行数据分析》