Python - 檢查 Pandas 索引是否僅包含布林值


要檢查 Pandas 索引是否僅包含布林值,請在 Pandas 中使用index.is_boolean()方法。首先,匯入所需的庫 -

import pandas as pd

建立 Pandas 索引

index = pd.Index([True, True, False, False, True, True, True])

顯示 Pandas 索引 -

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

檢查索引值是否只有布林值 -

print("\nIndex values only consists of booleans?\n",index.is_boolean())

示例

以下是程式碼 -

import pandas as pd

# Creating Pandas index
index = pd.Index([True, True, False, False, True, True, True])

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# check whether index values has only booleans
print("\nIndex values only consists of booleans?\n",index.is_boolean())

輸出

這將生成以下輸出 -

Pandas Index...
Index([True, True, False, False, True, True, True], dtype='object')

Number of elements in the index...
7

The dtype object...
object

Index values only consists of booleans?
True

更新於: 13-Oct-2021

76 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.