Pandas pd.isna() Function
pd.isna()is the most commonly used missing value detection function in Pandas. It checks whether each element is a missing value and returns a Boolean value (TrueorFalse) structure.
In data analysis, handling missing values is a very important first step.pd.isna()It can help you quickly identify missing positions in data, providing a basis for subsequent filling, deletion, or other processing operations.
Basic Syntax and Parameters
Syntax Format
pandas.isna(obj)
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| obj | DataFrame、Series、scalar、list、tuple | The object for which missing values need to be checked; it can be any supported data type. |
Return Value Description
- Returns a Boolean object with the same structure as the input:
- If input is DataFrame, returns a Boolean DataFrame
- If input is Series, returns a Boolean Series
- If input is a single value, returns a Boolean scalar
Example
Let us, through a series of examples from simple to complex, thoroughly masterpd.isna()its usage.
Example 1: Detecting Missing Values in Series
Example
import numpy as np
# Create a Series containing missing values
s = pd.Series([1, 2, np.nan, 4, None, 6])
# Detect missing values
result = pd.isna(s)
print("Original Series:")
print(s)
print("\n"Detection result:")
print(result)
Output:
原始 Series: 0 1.0 1 2.0 2 NaN 3 4.0 4 NaN 5 6.0 dtype: float64 检测结果: 0 False 1 False 2 True 3 False 4 True 5 False dtype: bool
Code explanation:
np.nanandNoneare all recognized as missing values- In the returned Boolean Series, missing value positions are
True, non-missing value positions areFalse
Example 2: Detecting Missing Values in DataFrame
Example
import numpy as np
# Create a DataFrame containing missing values
df = pd.DataFrame({
'A': [1, 2, np.nan, 4],
'B': ['a', None, 'c', 'd'],
'C': [pd.NaT, 6, 7, 8] # NaT is a missing value for time type
})
# Detect missing values
result = pd.isna(df)
print("Original DataFrame:")
print(df)
print("\n"Detection result:")
print(result)
Output:
原始 DataFrame:
A B C
0 1.0 a None
1 2.0 None 6
2 NaN c 7
3 4.0 d 8
dtype: object
检测结果:
A B C
0 False False True
1 False True False
2 True False False
3 False False False
Code explanation:
pd.NaT(Not a Time) is a missing value of time type, and it is also correctly identified- Every position in the DataFrame returns the corresponding Boolean value
Example 3: Counting Missing Values
In actual data analysis, we often need to know how many missing values there are.
Example
import numpy as np
# Create data
df = pd.DataFrame({
'A': [1, np.nan, 3, np.nan, 5],
'B': [np.nan, 2, np.nan, 4, np.nan],
'C': [1, 2, 3, 4, 5]
})
# Count missing values in each column
print("Number of missing values per column:")
print(pd.isna(df).sum())
print("\n"Total missing values:", pd.isna(df).sum().sum())
# Calculate missing value proportion
print("\n"Missing value proportion:")
print((pd.isna(df).sum() / len(df) * 100).round(2), "%")
Output:
每列缺失值数量: A 2 B 3 C 0 dtype: int64 总缺失值数量: 5 缺失值比例: A 40.0 B 60.0 C 0.0 %
Example 4: Combining with Other Functions
Example
import numpy as np
# Create data
df = pd.DataFrame({
'name': ['Alice', 'Bob', None, 'Diana', 'Eve'],
'age': [25, None, 35, 28, None],
'score': [85, 90, 78, 92, 88]
})
# Use isna to filter rows containing missing values
rows_with_na = df[pd.isna(df).any(axis=1)]
print("Rows containing missing values:")
print(rows_with_na)
# Use isna to fill missing values
df_filled = df.fillna({
'name': 'Unknown',
'age': df['age'].mean(), # Fill age with mean
'score': 0
})
print("\n"DataFrame after filling:")
print(df_filled)
Output:
包含缺失值的行:
name age score
0 Alice 25.0 85
2 None 35.0 78
3 Diana 28.0 92
填充后的 DataFrame:
name age score
0 Alice 25.0 85
1 Bob 29.0 90
2 Unknown 35.0 78
3 Diana 28.0 92
4 Eve 0.0 88
Important Notes
Important tips:
pd.isna()The following values will be recognized as missing values:
np.nan- Missing values of numeric typeNone- Python native None objectpd.NaT- Missing values of datetime type
For empty strings""or0,pd.isna()will returnFalse, because they are not missing values.
Other extensions
Pandas Common Functions