การตรวจสอบว่า Index ที่มี NaN เป็นชนิด floating
ใช้เมธอด index.is_floating() ตรวจสอบว่า Index เป็นชนิดข้อมูล floating (float64) แม้จะมีค่า NaN ปนอยู่
ขั้นตอน
- นำเข้า pandas และ numpy
- สร้าง Index ที่มี float และ NaN
- แสดง Index
- ใช้
is_floating()ตรวจสอบ
ตัวอย่างโค้ด
import pandas as pd
import numpy as np
# สร้าง Index มี float และ NaN
index = pd.Index([5.7, 6.8, 10.5, np.nan, 17.8, 25.6, np.nan, np.nan, 50.2])
# แสดง Index
print("Pandas Index...\n", index)
# แสดงจำนวนองค์ประกอบและ dtype
print("\nNumber of elements in the index...\n", index.size)
print("\nThe dtype object...\n", index.dtype)
# ตรวจสอบว่าเป็น floating type
print("\nIndex values with some NaNs is a floating type?\n", index.is_floating())
ผลลัพธ์
Pandas Index... Float64Index([5.7, 6.8, 10.5, nan, 17.8, 25.6, nan, nan, 50.2], dtype='float64') Number of elements in the index... 9 The dtype object... float64 Index values with some NaNs is a floating type? True
คำอธิบาย
NaN ใน Pandas เป็นค่า float ดังนั้น Index ที่มี float และ NaN จะเป็น Float64Index และ is_floating() คืนค่า True