Để kiểm tra xem Chỉ mục gấu trúc chỉ bao gồm dữ liệu số, hãy sử dụng index.is_numeric () phương pháp. Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd import numpy as np
Tạo chỉ mục Pandas với số nguyên, float và NaNs
index = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan])
Hiển thị chỉ mục Gấu trúc -
print("Pandas Index...\n",index) Kiểm tra xem các giá trị chỉ mục chỉ có dữ liệu số hay không. Dữ liệu số bao gồm số nguyên, số thực và số NaN -
index.is_numeric()
Ví dụ
Sau đây là mã -
import pandas as pd
import numpy as np
# Creating Pandas index with integer, float and NaNs
index = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan])
# 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 numeric data.
# Numeric data includes integer, floats and NaNs
print("\nIndex values only consists of numeric data?\n",index.is_numeric()) Đầu ra
Điều này sẽ tạo ra kết quả sau -
Pandas Index... Float64Index([5.0, 10.2, 25.0, 50.0, 75.2, 100.0, nan], dtype='float64') Number of elements in the index... 7 The dtype object... float64 Index values only consists of numeric data? True