必知必会Pandas Series基本属性和方法

1.Series的基本属性和方法

  • shape 形状

  • size 长度

  • index 索引

  • values 值

  • name 名字

# 导包import pandas as pds = pd.Series({    "语文":100,    "数学":150,    "英语":110,    "Python":130,    "Pandas":150,    "NumPy":150},name="考试成绩")s# 执行结果语文        100数学        150英语        110Python    130Pandas    150NumPy     150Name: 考试成绩, dtype: int64# 形状s.shape# 执行结果(6,)# 元素个数s.size# 执行结果6# 索引s.index# 执行结果Index(['语文', '数学', '英语', 'Python', 'Pandas', 'NumPy'], dtype='object')# 值s.values# 执行结果array([100, 150, 110, 130, 150, 150], dtype=int64)# 索引名字s.name# 执行结果'考试成绩'
  • head() 查看前几条数据,默认5条

  • tail() 查看后几条数据,默认5条

# 查看前几条数据,默认5条s.head()# 执行结果语文        100数学        150英语        110Python    130Pandas    150Name: 考试成绩, dtype: int64# 查看指定前面2条数据s.head(2)# 执行结果语文    100数学    150Name: 考试成绩, dtype: int64# 查看最后几条数据,默认5条s.tail()# 执行结果数学        150英语        110Python    130Pandas    150NumPy     150Name: 考试成绩, dtype: int64# 查看指定最后2条数据s.tail(2)# 执行结果Pandas    150NumPy     150Name: 考试成绩, dtype: int64

2.检测缺失数据

  • pd.isnull()

  • pd.notnull()

  • isnull()

  • notnull()

s = pd.Series(["张三","李四","王五",np.nan])s# 执行结果0     张三1     李四2     王五3    NaNdtype: object# isnull:判断是否为空s.isnull()# 执行结果0    False1    False2    False3     Truedtype: boolpd.isnull(s)# 执行结果0    False1    False2    False3     Truedtype: bool# notnull:判断是否不为空s.notnull()# 执行结果0     True1     True2     True3    Falsedtype: boolpd.notnull(s)# 执行结果0     True1     True2     True3    Falsedtype: bool

3.使用bool值索引过滤数据

# 第一种方式:过滤掉空值cond1 = s.isnull()cond1# ~ 取反s[~cond1]# 执行结果0    张三1    李四2    王五dtype: object# 第二种方式:过滤掉空值cond2 = s.notnull()cond2s[cond2]# 执行结果0    张三1    李四2    王五dtype: object

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾飞开源

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值