Để kiểm tra xem Pandas Index chỉ chứa boolean hay không, hãy sử dụng phương thức index.is_boolean() trong Pandas. Trước tiên, nhập các thư viện cần thiết:
import pandas as pd
Tạo Pandas Index:
index = pd.Index([True, True, False, False, True, True, True])
Hiển thị Pandas Index:
print("Pandas Index...\n", index)
Kiểm tra xem các giá trị index chỉ chứa boolean:
print("\nGiá trị index chỉ chứa boolean?\n", index.is_boolean())
Ví dụ
Dưới đây là mã hoàn chỉnh:
import pandas as pd
# Tạo Pandas Index
index = pd.Index([True, True, False, False, True, True, True])
# Hiển thị Pandas Index
print("Pandas Index...\n", index)
# Trả về số lượng phần tử trong Index
print("\nSố lượng phần tử trong index...\n", index.size)
# Trả về kiểu dữ liệu
print("\nĐối tượng dtype...\n", index.dtype)
# kiểm tra xem index values chỉ chứa boolean
print("\nGiá trị index chỉ chứa boolean?\n", index.is_boolean())
Kết quả
Đoạn mã trên sẽ cho ra kết quả sau:
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