หากต้องการตรวจสอบว่าดัชนีมี NaN หรือไม่ ให้ใช้ index.hasnans ทรัพย์สินในหมีแพนด้า
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd import numpy as np
การสร้างดัชนี สำหรับ NaN เราได้ใช้ numpy library -
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship'])
แสดงดัชนี -
print("Pandas Index...\n",index) ตรวจสอบว่าดัชนีมี NaN หรือไม่ -
print("\nIs the Pandas index having NaNs?\n",index.hasnans)
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
import numpy as np
# Creating the index
# For NaN, we have used numpy library
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship'])
# Display the index
print("Pandas Index...\n",index)
# Return an array representing the data in the Index
print("\nArray...\n",index.values)
# Check if the index is having NaNs
print("\nIs the Pandas index having NaNs?\n",index.hasnans)
# Return a tuple of the shape of the underlying data
print("\nA tuple of the shape of underlying data...\n",index.shape) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Pandas Index... Index(['Car', 'Bike', nan, 'Car', nan, 'Ship'], dtype='object') Array... ['Car' 'Bike' nan 'Car' nan 'Ship'] Is the Pandas index having NaNs? True A tuple of the shape of underlying data... (6,)