python pandas series_Python Pandas Series.all()用法及代码示例

Pandas 系列是带有轴标签的一维ndarray。标签不必是唯一的,但必须是可哈希的类型。该对象同时支持基于整数和基于标签的索引,并提供了许多方法来执行涉及索引的操作。

Pandas Series.all()函数返回是否所有元素都为True(可能在某个轴上)。它返回True除非系列中或沿Dataframe轴存在至少一个元素是False或等效值(例如零或为空)。

用法: Series.all(axis=0, bool_only=None, skipna=True, level=None, **kwargs)

参数:

axis:指出应减少的轴。

bool_only:仅包括布尔列。

skipna:排除NA /空值。

level:如果轴是MultiIndex(分层),则沿特定级别计数,并折叠成标量。

** kwargs:其他关键字无效,但可以接受与NumPy的兼容性。

返回:标量或系列

范例1:采用Series.all()函数检查给定系列对象中的所有值是否为True或非零。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series([34, 5, 13, 32, 4, 15])

# Create the Index

index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']

# set the index

sr.index = index_

# Print the series

print(sr)

输出:

Coca Cola 34

Sprite 5

Coke 13

Fanta 32

Dew 4

ThumbsUp 15

dtype:int64

现在我们将使用Series.all()用于检查给定系列对象中的所有值是否为True且非零的函数。

# check if all value is True

# or non-zero

result = sr.all()

# Print the result

print(result)

输出:

True

正如我们在输出中看到的,Series.all()函数已成功返回True指示给定系列中的所有值均为True或非零。

范例2:采用Series.all()函数检查给定系列对象中的所有值是否为True或非零。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series([51, 10, 24, 18, 1, 84, 12, 10, 5, 24, 0])

# Create the Index

# apply yearly frequency

index_ = pd.date_range('2010-10-09 08:45', periods = 11, freq ='Y')

# set the index

sr.index = index_

# Print the series

print(sr)

输出:

2010-12-31 08:45:00 51

2011-12-31 08:45:00 10

2012-12-31 08:45:00 24

2013-12-31 08:45:00 18

2014-12-31 08:45:00 1

2015-12-31 08:45:00 84

2016-12-31 08:45:00 12

2017-12-31 08:45:00 10

2018-12-31 08:45:00 5

2019-12-31 08:45:00 24

2020-12-31 08:45:00 0

Freq:A-DEC, dtype:int64

现在我们将使用Series.all()用于检查给定系列对象中的所有值是否为True且非零的函数。

# check if all value is True

# or non-zero

result = sr.all()

# Print the result

print(result)

输出:

False

正如我们在输出中看到的,Series.all()函数已成功返回False指示给定系列中的所有值都不是True或非零。在此系列对象中,值之一为零。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值