Python Pandas - 返回索引中是否有任何元素为 True
pythonpandasserver side programmingprogramming更新于 2025/12/31 2:52:17
要返回索引中的所有元素是否为 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
# 使用一些 True(非零)和 False(零)元素创建索引
index = pd.Index([15, 25, 0, 0, 55])
# 显示索引
print("Pandas Index...\n",index)
# 返回底层数据形状的元组
print("\nA底层数据形状的元组...\n",index.shape)
# 返回索引中的元素数量
print("\n索引中的元素数量...\n",index.size)
# 如果索引中的任何元素为 True,则返回 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,) 索引中的元素数量... 5 Check whether any element in the index is True... True
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

