首页 > 其他分享 >10.Series的访问

10.Series的访问

时间:2022-11-13 22:35:01浏览次数:43  
标签:10 False 索引 Series lucy 访问 tom

#其他访问方式,列表和numpy的访问方式完全适用于Series

arr =np.random.randint(0,100,size=5)
arr
array([38, 15, 10, 85, 81])
s=Series(data=arr,index=["tom", "lucy", "mery", "jack", "tony"])
s
tom     38
lucy    15
mery    10
jack    85
tony    81
dtype: int32
#5.使用带索引的bool型的Series列表访问 
#【注意】如果使用Series的Bool列表,要注意索引对齐,顺序不要求一致,内容要求一致 
s_bool = Series (data=[True, True, False, False,False], index=["tom", "lucy", "mery", "jack", "tony"]) 
#【注意】 bool列表和要访问的对象的长度保持一致 
n_bool = np. array ([True, True, False, False, False]) 
l_bool = [True, True, False, False, False]
s[s_bool]     #Series必须加索引才能访问,Series访问Series索引必须对齐,否则会混乱无法访问
tom     38
lucy    15
dtype: int32

#官方规定访问方式

(1) 显式索引:                                     ##所有使用标签(显示索引)  的都是闭区间
使用index中的元素作为索引值

-使用.loc[]   (推荐)

注意,此时是闭区间

(2)隐式索引:                                   ##所有使用隐式索引的都是开区间
-使用整数作为索引值

使用.iloc[] (推荐)

注意,此时是半开区间

#4.使用iloc,配合隐式索引访问,官方推荐的隐式索引的访问机制

#s. iloc [0]
s. iloc[[0, 1, 0, 1]]
tom     38
lucy    15
tom     38
lucy    15
dtype: int32
#3.使用1oc,配合显示索引,官方推荐的访问机制

#s. loc ["tom"]
s. loc[["tom","lucy"]]
tom     38
lucy    15
dtype: int32

 

标签:10,False,索引,Series,lucy,访问,tom
From: https://www.cnblogs.com/988MQ/p/16887210.html

相关文章