Computer >> Máy Tính >  >> Lập trình >> Python

Python Pandas - Trả về liệu bất kỳ phần tử nào trong chỉ mục là True

Để trả về liệu tất cả các phần tử trong chỉ mục có đúng hay không, hãy sử dụng index.any () trong Pandas.

Đầu tiên, hãy nhập các thư viện được yêu cầu -

import pandas as pd

Tạo chỉ mục với một số phần tử True (khác 0) và False (không) -

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

Hiển thị chỉ mục -

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

Trả về True nếu bất kỳ phần tử nào trong chỉ mục là True:-

print("\nCheck whether any element in the index is True...\n",index.any())

Ví dụ

Sau đây là mã -

import pandas as pd

# Creating the index with some True (non-zero) and False (zero) elements
index = pd.Index([15, 25, 0, 0, 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 any element in the index is True
print("\nCheck whether any element in the index is True...\n",index.any())

Đầu ra

Điều này sẽ tạo ra mã sau -

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

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

Number of elements in the index...
5

Check whether any element in the index is True...
True