要使用pandas,你首先得熟悉他的两个数据结构:Series和Dataframe。
Series
series 是一种类似于 一维数组的的对象,他由一组数据以及与之相关的数据标签组成。
In [42]: importpandas as pd
In [43]: pd.Series([2,3,7,1])
Out[43]:
02
1 3
2 7
3 1dtype: int64
In [44]:
In [44]: f=pd.Series([2,3,7,1])
In [45]: f.index
Out[45]: RangeIndex(start=0, stop=4, step=1)
In [46]: f.values
Out[46]: array([2, 3, 7, 1], dtype=int64)
可以将series看成是一个有序的字典,因为它是索引值到数据值的一个映射,而且它具有很多字典的特性。
可以通过索引的方式选取其中的一个或一组值:
In [56]: f
Out[56]:
fang3liu9wang2su3dtype: int64
In [57]: f['fang']
Out[57]: 3In [58]: f[['fang','su','liu']]
Out[58]:
fang3su3liu9dtype: int64
同时支持python当中的in
In [56]: f
Out[56]:
fang3liu9wang2su3dtype: int64
In [59]: 'fang' inf
Out[59]: True
In [60]: 'liu' inf
Out[60]: True
In [61]: 'laohu' inf
Out[61]: False
In [62]: 9 inf
Out[62]: False
而且两个Series 直接通过相同的index进行合并,请看下面的例子
In [49]: f=pd.Series([3,9,2,3],index=['fang','liu','wang','su'])
In [50]: f
Out[50]:
fang3liu9wang2su3dtype: int64
In [51]: d=pd.Series([2,22,13,14],index=['fang','liu','wang','su'])
In [52]: d+f
Out[52]:
fang5liu31wang15su17dtype: int64
同时假如你有一个python的字典类型可以将其直接转换为series
In [54]: o={'fang':567,'su':456,'liu':110}
In [55]: pd.Series(o)
Out[55]:
fang567su456liu110dtype: int64
Series对象本身以及它的索引有一个name属性
In [65]: f.name='count'In [66]: f.index.name='people name'In [67]: f
Out[67]:
people name
fang3liu9wang2su3Name: count, dtype: int64
In [68]:
Series的index可以通过赋值进行修改
In [69]: f.index=['zhou','wu','zheng','wang']
In [70]: f
Out[70]:
zhou3wu9zheng2wang3Name: count, dtype: int64
Dataframe
dataframe 是一个表格行的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值,字符串,布尔值等),相比于numpy的数组一般是统一的数据类型,dataframe相比于series会有两个索引,行索引和列索引。
创建dataframe
创建dataframe的方式有很多种,但是最常见的也是最直观的是通过等长的列表或者数组组成的字典去创建。
In [76]: data
Out[76]:
{'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],'year': [2000, 2001, 2002, 2001, 2002, 2003],'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
In [77]: pd.DataFrame(data)
Out[77]:
state year pop
0 Ohio2000 1.5
1 Ohio 2001 1.7
2 Ohio 2002 3.6
3 Nevada 2001 2.4
4 Nevada 2002 2.9
5 Nevada 2003 3.2
另一种是创建嵌套字典,如果嵌套字典传给Dataframe,pandas就会被解释为外层字典的键作为列,内层的键作为作为行索引。
In [130]: pop = {'Nevada': {2001: 2.4, 2002: 2.9},'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
In [131]: pd.DataFrame(pop)
Out[131]:
Nevada Ohio2001 2.4 1.7
2002 2.9 3.6
2000 NaN 1.5
可以在创建时指定列的顺序
In [78]: pd.DataFrame(data,columns=['year','state','pop'])
Out[78]:
year state pop
02000 Ohio 1.5
1 2001 Ohio 1.7
2 2002 Ohio 3.6
3 2001 Nevada 2.4
4 2002 Nevada 2.9
5 2003 Nevada 3.2
dataframe 除了有columns以外同样也有index序号,可以在创建时赋值
In [86]: data
Out[86]:
{'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],'year': [2000, 2001, 2002, 2001, 2002, 2003],'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
In [87]: frame=pd.DataFrame(data,columns=['year','state','pop'],index=['one','two','three','four','five','six'])
In [88]: frame
Out[88]:
year state pop
one2000 Ohio 1.5two2001 Ohio 1.7three2002 Ohio 3.6four2001 Nevada 2.4five2002 Nevada 2.9six2003 Nevada 3.2
获取dataframe的index和columns列表
In [89]: frame.index.to_list()
Out[89]: ['one', 'two', 'three', 'four', 'five', 'six']
In [90]: frame.columns.to_list()
Out[90]: ['year', 'state', 'pop']
像series一样,dataframe也都有name属性
In [93]: frame.index.name='num'In [94]: frame.index.name
Out[94]: 'num'In [95]: frame
Out[95]:
year state pop
num
one2000 Ohio 1.5two2001 Ohio 1.7three2002 Ohio 3.6four2001 Nevada 2.4five2002 Nevada 2.9six2003 Nevada 3.2In [96]: frame.columns.name='column_name'In [97]: frame.columns.name
Out[97]: 'column_name'In [98]: frame
Out[98]:
column_name year state pop
num
one2000 Ohio 1.5two2001 Ohio 1.7three2002 Ohio 3.6four2001 Nevada 2.4five2002 Nevada 2.9
赋值
dataframe当中的行与列进行赋值,需要注意的是当你想为一整列或者一整行赋给一个同样的值那么直接赋值就可以
In [108]: frame
Out[108]:
column_name year state pop
num
one2000 Ohio 1.5two2001 Ohio 1.7three2002 Ohio 3.6four2001 Nevada 2.4five2002 Nevada 2.9six2003 Nevada 3.2In [109]: frame['pop'] =22In [110]: frame
Out[110]:
column_name year state pop
num
one2000 Ohio 22two2001 Ohio 22three2002 Ohio 22four2001 Nevada 22five2002 Nevada 22six2003 Nevada 22In [118]: frame.loc['one']=23In [119]: frame
Out[119]:
column_name year state pop
num
one23 23 23two2001 Ohio 22three2002 Ohio 22four2001 Nevada 22five2002 Nevada 22six2003 Nevada 22
但是当你想为dataframe当中的行或列赋予不同的值时,需要注意的是 你需要传入与当前行列等长的列表
In [113]: frame.loc['one']=22,'fang','wang'In [114]: frame
Out[114]:
column_name year state pop
num
one22fang wang
two2001 Ohio 22three2002 Ohio 22four2001 Nevada 22five2002 Nevada 22six2003 Nevada 22
In [122]: frame['pop']=1,2,3,4,5,6In [123]: frame
Out[123]:
column_name year state pop
num
one23 23 1two2001 Ohio 2three2002 Ohio 3four2001 Nevada 4five2002 Nevada 5six2003 Nevada 6
为不存在的行或者列赋值时会直接创建一个新的行或者列,需要注意的是同样需要传入与当前行,列等长的
In [123]: frame
Out[123]:
column_name year state pop
num
one23 23 1two2001 Ohio 2three2002 Ohio 3four2001 Nevada 4five2002 Nevada 5six2003 Nevada 6In [128]: frame.loc['one']=range(4)
In [129]: frame
Out[129]:
column_name year state pop ID
num
one 01 2 3two2001 Ohio 2 1three2002 Ohio 3 2four2001 Nevada 4 3five2002 Nevada 5 4six2003 Nevada 6 5In [125]: frame['ID']=range(6)
In [126]: frame
Out[126]:
column_name year state pop ID
num
one23 23 10
two2001 Ohio 2 1three2002 Ohio 3 2four2001 Nevada 4 3five2002 Nevada 5 4six2003 Nevada 6 5
与python的集合不同,pandas的index可以包含重复项
In [136]: f=pd.DataFrame(data,index=[1,1,3,4,5,6])
In [137]: f
Out[137]:
state year pop1 Ohio 2000 1.5
1 Ohio 2001 1.7
3 Ohio 2002 3.6
4 Nevada 2001 2.4
5 Nevada 2002 2.9
6 Nevada 2003 3.2In [138]: f.loc[1]
Out[138]:
state year pop1 Ohio 2000 1.5
1 Ohio 2001 1.7
以上就是pandas当中的series和dataframe这两种数据结构的基本方法和属性,后面我们会继续介绍操作他们的其他手段以及pandas在数据处理和数据分析上的其他功能。