# 如何檢查 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.