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,)

更新於: 13-Oct-2021

1K+ 瀏覽

開啟你的 職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.