📜  Python|熊猫系列.all()

📅  最后修改于: 2022-05-13 01:54:22.157000             🧑  作者: Mango

Python|熊猫系列.all()

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

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

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

# 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()函数来检查给定系列对象中的所有值是否为真且非零。

# 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()函数检查给定系列对象中的所有值是否为真或非零。

# 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()函数来检查给定系列对象中的所有值是否为真且非零。

# check if all value is True
# or non-zero
result = sr.all()
  
# Print the result
print(result)

输出 :

False

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