>>> import pandas>>> import numpy as np>>> from pandas import Series,DataFrame#define a series without assigned index>>> obj = Series([1,-5,7,3])>>> print obj0 11 -52 73 3dtype: int64>>> print obj.indexRangeIndex(start=0, stop=4, step=1)>>> print obj.values[ 1 -5 7 3]>>> print obj[3]3#explicitly assigned index dbac>>> obj1 = Series([1,2,3,4],index=['d','b','a','c'])>>> print obj1d 1b 2a 3c 4dtype: int64>>> print obj1.values[1 2 3 4]>>> print obj1.indexIndex([u'd', u'b', u'a', u'c'], dtype='object')>>> print obj1['c']4>>> obj1['a']=-4>>> print obj1.values[ 1 2 -4 4]#basic operation, index will not be changed>>> obj1[obj1>0]d 1b 2c 4dtype: int64>>> print obj1d 1b 2a -4c 4dtype: int64>>> obj2 = obj1[obj1>0]>>> obj2d 1b 2c 4dtype: int64>>> obj2*2d 2b 4c 8dtype: int64>>> obj2d 1b 2c 4dtype: int64>>> obj2 = obj2*2>>> obj2d 2b 4c 8dtype: int64>>> obj2=np.exp(obj2)>>> obj2d 7.389056b 54.598150c 2980.957987dtype: float64>>> 'b' in obj2True>>> 'e' in obj2False
给Series赋值index和values
#define a Series with indexes and values>>> sdata={'beijing':'010','shanghai':'021','guangdong':'020'}>>> obj3 = Series(sdata)>>> print obj3beijing 010guangdong 020shanghai 021dtype: object>>> index1 = ['tianjin','shanghai','guangdong','beijing']>>> obj3 = Series(sdata,index=index1)>>> print obj3tianjin NaNshanghai 021guangdong 020beijing 010dtype: object#isnull or notnull>>> import pandas as pd>>> print pd.isnull(obj3)tianjin Trueshanghai Falseguangdong Falsebeijing Falsedtype: bool>>> print pd.notnull(obj3)tianjin Falseshanghai Trueguangdong Truebeijing Truedtype: bool
将乱序索引的两个Series根据索引相加
>>> obj3 = Series(sdata)>>> print obj3beijing 010guangdong 020shanghai 021dtype: object>>> index1 = ['tianjin','shanghai','guangdong','beijing']>>> obj4 = Series(sdata,index=index1)>>> print obj4tianjin NaNshanghai 021guangdong 020beijing 010dtype: object>>> print obj3+obj4beijing 010010guangdong 020020shanghai 021021tianjin NaNdtype: object
Series name and index name
>>> obj4.name='postcode'>>> obj4.index.name='city'>>> print obj4citytianjin NaNshanghai 021guangdong 020beijing 010Name: postcode, dtype: object