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')