Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python - ตรวจสอบว่าดัชนี Pandas ประกอบด้วยบูลีนเท่านั้นหรือไม่


ในการตรวจสอบว่าดัชนี Pandas ประกอบด้วยบูลีนเท่านั้นหรือไม่ ให้ใช้ index.is_boolean() วิธีการในแพนด้า ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

การสร้างดัชนี Pandas

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

แสดงดัชนีหมีแพนด้า −

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