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.