Python Pandas - 檢查索引中是否存在缺失值
要檢查索引中是否存在缺失值,請在 Pandas 中使用 index.hasnans 屬性。
首先,匯入所需的庫 −
import pandas as pd import numpy as np
建立索引。對於 NaN,我們使用了 numpy 庫 −
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship'])
顯示索引 −
print("Pandas Index...\n",index)檢查索引中是否有缺失值 −
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,)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP