series的基本用法
1、is null 和 not null 检查缺失值
s4.isnull() #判断是否为空,空就是True
city False
name False
age False
sex True
dtype: bool
s4.notnull() # 判断是否不为空,非空就是True
city True
name True
age True
sex False
dtype: bool
#返回一个Series对象
2、索引和切片
s5 = pd.Series(np.random.rand(5),index=['a','b','c','d','e'])
s5
a 0.968340
b 0.727041
c 0.607197
d 0.134053
e 0.240239
dtype: float64
# 下标
s5[1] #通过下标获取到元素,不能倒着取,和我们python列表不一样, s5[-1]错误的写法
0.7270408328885498
#通过标签名
s5['c']
0.6071966171492978
#选取多个,还是Series
s5[[1,3]] 或 s5[['b','d']] # [1,3] 或['b','d']是索引列表
b 0.727041
d 0.134053
dtype: float64
#切片 标签切片包含末端数据(指定了标签)
s5[1:3]
b 0.727041
c 0.607197
dtype: float64
s5['b':'d']
b 0.727041
c 0.607197
d 0.134053
dtype: float64
#布尔索引
s5[s5>0.5] #保留为True的数据
a 0.968340
b 0.727041
c 0.607197
dtype: float64
3、name属性
#Series对象本身和其本身索引都具有name属性
s6 = pd.Series({'apple':7.6,'banana':9.6,'watermelon':6.8,'orange':3.6})
s6.name = 'fruit_price' # 设置Series对象的name属性
s6.index.name = 'fruit' # 设置索引name属性
s6
fruit
apple 7.6
banana 9.6
watermelon 6.8
orange 3.6
Name: fruit_price, dtype: float64
#查看索引
s6.index
Index(['apple', 'banana', 'watermelon', 'orange'], dtype='object', name='fruit')