Series

构造Series

obj=Series([4,5,-7,7])

obj
Out[140]: 
0    4
1    5
2   -7
3    7
dtype: int64

obj.index
Out[141]: RangeIndex(start=0, stop=4, step=1)

obj.values
Out[142]: array([ 4,  5, -7,  7], dtype=int64)

构造同时指定索引

obj2=Series([4,5,-7,7],index=['a','b','c','d'])

obj2
Out[144]: 
a    4
b    5
c   -7
d    7
dtype: int64

读取

obj2.a
Out[148]: 4

obj2['a']
Out[149]: 4

数学运算

obj2[obj2>0]
Out[150]: 
a    4
b    5
d    7
dtype: int64

obj2*2
Out[151]: 
a     8
b    10
c   -14
d    14
dtype: int64

np.exp(obj2)
Out[152]: 
a      54.598150
b     148.413159
c       0.000912
d    1096.633158
dtype: float64
'b' in obj2
Out[153]: True

't' in obj2
Out[154]: False

由字典变成Series

sdata={'ohio':3500,'texas':71000,'oregon':16000,'utah':500}

obj3=Series(sdata)

obj3
Out[159]: 
ohio       3500
oregon    16000
texas     71000
utah        500
dtype: int64

替换字典的index,与California 对应的sdata 没有,显示是NaN

states=['california','ohio','oregon','texas']

sdata={'ohio':3500,'texas':71000,'oregon':16000,'utah':500}

obj4=Series(sdata,index=states)

obj4
Out[170]: 
california        NaN
ohio           3500.0
oregon        16000.0
texas         71000.0
dtype: float64

判断数据是否缺失

pd.isnull(obj4)
Out[171]: 
california     True
ohio          False
oregon        False
texas         False
dtype: bool

pd.notnull(obj4)
Out[172]: 
california    False
ohio           True
oregon         True
texas          True
dtype: bool

判断缺失的另一种表达

obj4.notnull()
Out[176]: 
california    False
ohio           True
oregon         True
texas          True
dtype: bool

obj4.isnull()
Out[177]: 
california     True
ohio          False
oregon        False
texas         False
dtype: bool
obj3
Out[18]: 
ohio       3500
oregon    16000
texas     71000
utah        500
dtype: int64

obj4
Out[19]: 
california        NaN
ohio           3500.0
oregon        16000.0
texas         71000.0
dtype: float64

obj3+obj4
Out[20]: 
california         NaN
ohio            7000.0
oregon         32000.0
texas         142000.0
utah               NaN
dtype: float64
obj4.name='population'

obj4.index.name='state'

obj4
Out[30]: 
state
california        NaN
ohio           3500.0
oregon        16000.0
texas         71000.0
Name: population, dtype: float64
obj=Series([4,7,-5,3])

obj
Out[43]: 
0    4
1    7
2   -5
3    3
dtype: int64

obj.index=['bob','steve','jeff','ryan']

obj
Out[45]: 
bob      4
steve    7
jeff    -5
ryan     3
dtype: int64
posted @ 2022-08-19 23:00  luoganttcc  阅读(167)  评论(0编辑  收藏  举报