Để kiểm tra xem chỉ mục có NaN hay không, hãy sử dụng index.hasnans tài sản ở Pandas.
Đầ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. Đối với NaN, chúng tôi đã sử dụng thư viện numpy -
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship'])
Hiển thị chỉ mục -
print("Pandas Index...\n",index) Kiểm tra xem chỉ mục có NaN không -
print("\nIs the Pandas index having NaNs?\n",index.hasnans)
Ví dụ
Sau đây là mã -
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) Đầu ra
Điều này sẽ tạo ra mã sau -
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,)