Python Pandas - 返回索引中的所有元素是否均為 True


要返回索引中的所有元素是否都為 True,請在 Pandas 中使用 index.all() 方法。

首先,匯入所需的庫 −

import pandas as pd

建立索引 −

index = pd.Index([15, 25, 35, 45, 55])

顯示索引 −

print("Pandas Index...\n",index)

如果索引中的所有元素都為 True 則返回 True −

print("\nCheck whether all elements are True...\n",index.all())

示例

以下是程式碼 −

import pandas as pd

# Creating the index
index = pd.Index([15, 25, 35, 45, 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 all the elements in the index are True
print("\nCheck whether all elements are True...\n",index.all())

輸出

這將生成以下程式碼 −

Pandas Index...
Int64Index([15, 25, 35, 45, 55], dtype='int64')

A tuple of the shape of underlying data...
(5,)

Number of elements in the index...
5

Check whether all elements are True...
True

更新於: 2021 年 10 月 13 日

93 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

立即開始
廣告