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或非零。在此系列对象中,值之一为零。