python开发笔记-变长字典Series的使用

   Series的基本特征:

1、类似一维数组的对象

2、由数据和索引组成

import pandas as pd  
>>> aSer=pd.Series([1,2.0,'a'])  
>>> aSer  
0    1  
1    2  
2    a  
dtype: object  

  

bSer=pd.Series(['apple','peach','lemon'],index=[1,2,3])  
>>> bSer  
1    apple  
2    peach  
3    lemon  
dtype: object  
>>> bSer.index  
Int64Index([1, 2, 3], dtype='int64')  
>>> bSer.values  
array(['apple', 'peach', 'lemon'], dtype=object)  

  Series的基本运算:

    

from pandas import Series  
>>> aSer=Series([3,5,7],index=['a','b','c'])  
>>>   
>>> aSer['b']  
5  
aSer*2  
a     6  
b    10  
c    14  
dtype: int64  
>>> import numpy as np  
>>> np.exp(aSer)  
a      20.085537  
b     148.413159  
c    1096.633158  
dtype: float64  

  Series的数据对齐:

     

import pandas as pd  
>>> data={'AXP':'86.40','CSCO':'122.64','BA':'99.44'}  
>>> sindex=['AXP','CSCO','BA','AAPL']  
>>> aSer=pd.Series(data,index=sindex)  
>>> aSer  
AXP      86.40  
CSCO    122.64  
BA       99.44  
AAPL       NaN  
dtype: object  
>>> pd.isnull(aSer)  
AXP     False  
CSCO    False  
BA      False  
AAPL     True  
dtype: bool  

  重要功能:在算术运算中自动对齐不同索引的数据。

    

aSer=pd.Series(data,index=sindex)  
>>> aSer  
AXP      86.40  
CSCO    122.64  
BA       99.44  
AAPL       NaN  
dtype: object  
>>> bSer={'AXP':'86.40','CSCO':'122.64','CVX':'23.78'}  
cSer=pd.Series(bSer)  
>>> aSer+cSer  
AAPL             NaN  
AXP       86.4086.40  
BA               NaN  
CSCO    122.64122.64  
CVX              NaN  
dtype: object  

  

Series的name属性:

1、Series对象本身及其索引均有一个name属性

2、Series的name属性与其他功能关系密切

   

import pandas as pd  
>>> data={'AXP':'86.40','CSCO':'122.64','BA':'99.44'}  
>>> sindex=['AXP','CSCO','BA','AAPL']  
>>> aSer=pd.Series(data,index=sindex)  
>>> aSer.name='cnames'  
>>> aSer.index.name='volume'  
>>> aSer  
volume  
AXP      86.40  
CSCO    122.64  
BA       99.44  
AAPL       NaN  
Name: cnames, dtype: object  

  

posted @ 2018-03-14 08:23  圆柱模板  阅读(497)  评论(0编辑  收藏  举报