series的基本用法

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')
posted @   坤坤无敌  阅读(1440)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示