หากต้องการตรวจสอบว่าดัชนี Pandas มีเฉพาะข้อมูลที่เป็นตัวเลขหรือไม่ ให้ใช้ index.is_numeric() กระบวนการ. ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd import numpy as np
การสร้างดัชนี Pandas ด้วยจำนวนเต็ม float และ NaN
index = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan])
แสดงดัชนีหมีแพนด้า −
print("Pandas Index...\n",index) ตรวจสอบว่าค่าดัชนีมีเฉพาะข้อมูลตัวเลขหรือไม่ ข้อมูลตัวเลขประกอบด้วยจำนวนเต็ม ทศนิยม และ NaN -
index.is_numeric()
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
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()) ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
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