Python Pandas – 返回索引中是否存在任何元素为True的情况
要返回索引中所有元素是否都为True,请使用Pandas中的 index.any() 方法。
首先,导入所需的库 −
import pandas as pd
创建具有一些True(非零)和False(零)元素的索引 −
index = pd.Index([15, 25, 0, 0, 55])
显示索引 −
print("Pandas Index...\n",index)
如果索引中存在任何元素为True,则返回True :−
print("\n检查索引中是否存在任何元素为True的情况...\n",index.any())
示例
以下是代码−
import pandas as pd
# Creating the index with some True (non-zero) and False (zero) elements
index = pd.Index([15, 25, 0, 0, 55])
# Display the index
print("Pandas Index...\n",index)
# Return a tuple of the shape of the underlying data
print("\nA tuple of the shape of underlying data...\n",index.shape)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# Return True if any element in the index is True
print("\nCheck whether any element in the index is True...\n",index.any())
输出
这将产生以下输出−
Pandas Index...
Int64Index([15, 25, 0, 0, 55], dtype='int64')
A tuple of the shape of underlying data...
(5,)
Number of elements in the index...
5
Check whether any element in the index is True...
True
极客教程