# 如何检查 NaN 是否存在于 Pandas DataFrame 中

Suraj Joshi 2023年1月30日

`NaN` 代表不是数字 - `Not a Number`，表示 Pandas 中缺少的值。要在 Python Pandas 中检测 NaN 值，我们可以对 DataFrame 对象使用 `isnull()``isna()` 方法。

## pandas.DataFrame.isnull()方法

``````import pandas as pd
import numpy as np

df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)

df_check = df.isnull()

print(df_check)
``````

``````   Student  Height  Weight
0    False   False    True
1    False   False   False
2    False    True   False
3    False    True    True
4    False   False   False
``````

``````import pandas as pd
import numpy as np

df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)

check_for_nan = df.isnull().values.any()
print(check_for_nan)
``````

``````True
``````

`df.isnull().values` 返回 DataFrame 的 NumPy 表示形式。如果任何元素被评估为 True，那么 `numpy.any()` 返回 True。

### `df.isnull().any().any()` 检查是否存在任何 NaN

`df.any()` 返回任何元素是否为 True。当 `df` 是一个 DataFrame 时，它返回一个 `pd.Series`；而当 `df``pd.Series` 时，它返回一个布尔值。

``````import pandas as pd
import numpy as np

df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)

check_for_nan = df.isnull().any().any()

print(check_for_nan)
``````

``````True
``````

### `isnull().sum().sum()` 检查是否存在 NaN

``````import pandas as pd
import numpy as np

df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)

total_nan_values = df.isnull().sum().sum()
print(total_nan_values)
``````

``````4
``````

## `pandas.DataFrame.isna()` 方法

`pandas.DataFrame.isna()` 方法类似于
pandas.DataFrame.isnull()。两种方法的工作之间没有任何区别。它们只是名称不同。

``````import pandas as pd
import numpy as np

df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)

df_check = df.isna()

check_for_any_nan = df.isna().values.any()
# Or
check_for_any_nan = df.isna().any().any()
total_nan_values = df.isna().sum().sum()
print(df_check)
print("NaN Presence:" + str(check_for_any_nan))
print("Total Number of NaN values:" + str(total_nan_values))
``````

``````   Student  Height  Weight
0    False   False    True
1    False   False   False
2    False    True   False
3    False    True    True
4    False   False   False
NaN Presence:True
Total Number of NaN values:4
``````

Suraj Joshi is a backend software engineer at Matrice.ai.