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("\nCheck whether any element in the index is 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
广告