Pandas pd.isna() function
pd.isna() is the most commonly used missing-value detection function in Pandas. It checks whether each element is missing and returns a boolean (True or False) structure.
In data analysis, handling missing values is a very important first step.pd.isna() It helps you quickly identify where values are missing in the data, providing a basis for subsequent filling, deletion, or other processing operations.
Basic syntax and parameters
Syntax format
pandas.isna(obj)
Parameter Description
| Parameters | Type | Description |
|---|---|---|
| obj | DataFrame、Series、scalar、list、tuple | The object to check for missing values, which can be any supported data type. |
Return value description
- Returns a boolean object with the same structure as the input:
- If the input is a DataFrame, a boolean DataFrame is returned
- If the input is a Series, a boolean Series is returned
- If the input is a single value, a boolean scalar is returned
Example
Let us thoroughly master pd.isna() through a series of examples from simple to complex.
Example 1: Detect missing values in a Series
Example
import
pandas
as
pd
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
)
(
"Original Series:"
)
(
s
)
(
"\nDetection result:"
)
(
result
)
Result:
原始 Series:
0 1.0
1 2.0
2 NaN
3 4.0
4 NaN
5 6.0
dtype: float64
Detection results:
0 False
1 False
2 True
3 False
4 True
5 False
dtype: bool
Code analysis:
np.nanandNoneare all identified as missing values- In the returned boolean Series, missing-value positions are
True, and non-missing positions areFalse
Example 2: Detect missing values in a DataFrame
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame with missing values
df
=
pd.
DataFrame
(
{
'A'
:
[
1
,
2
,
np.
nan
,
4
]
,
'B'
:
[
'a'
,
None
,
'c'
,
'd'
]
,
'C'
:
[
pd.
NaT
,
6
,
7
,
8
]
NaT is the missing value for time types
}
)
Detect missing values
result
=
pd.
isna
(
df
)
(
"Original DataFrame:"
)
(
df
)
(
"\nDetection result:"
)
(
result
)
Result:
原始 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
Detection results:
A B C
0 False False True
1 False True False
2 True False False
3 False False False
Code analysis:
pd.NaT(Not a Time) is the missing value for time types and is also correctly identified- Each position in the DataFrame returns the corresponding boolean value
Example 3: Count missing values
In real data analysis, we often need to know how many missing values there are.
Example
import
pandas
as
pd
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 the number of missing values in each column
(
"Missing value count per column:"
)
(
pd.
isna
(
df
)
.
sum
(
)
)
(
"\nTotal missing value count:"
,
pd.
isna
(
df
)
.
sum
(
)
.
sum
(
)
)
Compute the proportion of missing values
(
"\nMissing value proportion:"
)
(
(
pd.
isna
(
df
)
.
sum
(
)
/
len
(
df
)
100
)
.
round
(
2
)
,
"%"
)
Result:
Number of missing values per column:
A 2
B 3
C 0
dtype: int64
总缺失值数量: 5
Missing value ratio:
A 40.0
B 60.0
C 0.0
%
Example 4: Use in combination with other functions
Example
import
pandas
as
pd
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 out rows containing missing values
rows_with_na
=
df
[
pd.
isna
(
df
)
.
any
(
axis
=
1
)
]
(
"Rows containing missing values:"
)
(
rows_with_na
)
Use isna to fill missing values
df_filled
=
df.
fillna
(
{
'name'
:
'Unknown'
,
'age'
: df
[
'age'
]
.
mean
(
)
,
Fill age with the mean value
'score'
:
0
}
)
(
"\nFilled DataFrame:"
)
(
df_filled
)
Result:
Rows containing missing values:
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
Notes
Important note:
pd.isna()recognizes the following values as missing:
np.nan - Numeric missing valuesNone - Python's native None objectpd.NaT - Missing values of time type (datetime)For empty strings "" or 0,pd.isna() returns False, because they are not missing values.
Common Pandas functions