Pandas pd.notna() function
pd.notna() is a function in the Pandas library used toDetect missing valuesfunction. It checks each element in the input object to determine whether it is a non-missing value, and returns a result consisting of Boolean values (True/False).
This is one of the most commonly used functions in data cleaning; it helps us quickly identify valid values in the data and prepare for subsequent processing and analysis.
Word meaning: not is a negation,na is short for "not available"; together they mean "not a missing value".
Basic syntax and parameters
pd.notna() is a top-level function of the Pandas library; it can be called directly via pd.notna() , or via the .notna() method of a Series or DataFrame object.
Syntax format
pd.notna(obj)
Parameter Description
- Parameters:
obj - Type: any Python object, such as a Series, DataFrame, list, array, or scalar value.
- Description: the object to check for non-missing values. Missing values in Pandas include
None、NaN、NaT(missing time type) andpandas.NA。
Function description
- Return Value: returns a Boolean object with the same shape as the input object. If the input is a Series or DataFrame, an object of the same type is returned; if it is a scalar, a single Boolean value is returned.
- effect: returns True for non-missing values
Trueand False for missing values.False。
Example
Let us thoroughly master pd.notna() through a series of examples from simple to complex.
Example 1: basic usage - detecting missing values in scalars and lists
Example
import
pandas
as
pd
import
numpy
as
np
1. Check whether a scalar value is non-missing
(
"=== scalar check ==="
)
(
f
"pd.notna(10): {pd.notna(10)}"
)
normal number -> True
(
f
"pd.notna('runoob'): {pd.notna('runoob')}"
)
normal string -> True
(
f
"pd.notna(None): {pd.notna(None)}"
)
None -> False
(
f
"pd.notna(np.nan): {pd.notna(np.nan)}"
)
np.nan -> False
2. Detect missing values in a list
(
"\n=== list check ==="
)
data_list
=
[
1
,
2
,
np.
nan
,
'runoob'
,
None
,
5
]
result
=
pd.
notna
(
data_list
)
(
f
"original list: {data_list}"
)
(
f
"detection result: {result.tolist()}"
)
Convert to a list for easier viewing
Expected output:
=== 标量检测 ===
pd.notna(10): True
pd.notna('runoob'): True
pd.notna(None): False
pd.notna(np.nan): False
=== 列表检测 ===
原始列表: [1, 2, nan, 'runoob', None, 5]
检测结果: [True, False, True, False, True]
Code analysis:
pd.notna(10)andpd.notna('runoob')Detecting normal numbers and strings returnsTrue。pd.notna(None)andpd.notna(np.nan)detecting Python and NumPy missing-value markers returnsFalse。- Using
pd.notna()on a list returns a list of Booleans, with missing-value positions marked asFalse。
Example 2: detecting missing values in a Series
When working with Series data,pd.notna() can quickly locate valid data.
Example
import
pandas
as
pd
import
numpy
as
np
Create a Series containing missing values
s
=
pd.
Series
(
[
1
,
2
,
np.
nan
,
4
,
None
,
'runoob'
,
np.
nan
]
)
(
"=== Original Series ==="
)
(
s
)
(
f
"\nType: {type(s)}"
)
Detect using pd.notna()
(
"\n=== pd.notna() detection result ==="
)
result
=
pd.
notna
(
s
)
(
result
)
Using the Series notna() method (equivalent effect)
(
"\n=== s.notna() method detection ==="
)
(
s.
notna
(
)
)
Filter out non-missing values
(
"\n=== filter non-missing values ==="
)
(
s
[
s.
notna
(
)
]
)
Expected output:
=== 原始 Series ===
0 1
1 2
2 NaN
3 4
4 None
5 runoob
6 NaN
dtype: object
=== pd.notna() 检测结果 ===
0 True
1 True
2 False
3 True
4 False
5 True
6 False
dtype: bool
=== s.notna() 方法检测 ===
0 True
1 True
2 False
3 True
4 False
5 True
6 False
dtype: bool
=== 筛选非缺失值 ===
0 1
1 2
3 4
5 runoob
dtype: object
Code analysis:
pd.notna(s)ands.notna()both can detect missing values in a Series and return a Boolean Series.- Using Boolean indexing
s[s.notna()]you can quickly filter out all non-missing elements, which is very practical in data cleaning.
Example 3: detecting missing values in a DataFrame
When working with tabular data,pd.notna() can quickly show how complete the data is.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame with missing values
df
=
pd.
DataFrame
(
{
'name'
:
[
'Alice'
,
'Bob'
,
None
,
'Diana'
]
,
'age'
:
[
25
,
np.
nan
,
30
,
28
]
,
'score'
:
[
85
,
90
,
np.
nan
,
95
]
}
)
(
"=== Original DataFrame ==="
)
(
df
)
Detect missing values in the entire DataFrame
(
"\n=== pd.notna() detection result ==="
)
(
pd.
notna
(
df
)
)
Count missing values by column
(
"\n=== non-missing value count per column ==="
)
(
df.
notna
(
)
.
sum
(
)
)
Count missing values by row
(
"\n=== non-missing value count per row ==="
)
(
df.
notna
(
)
.
sum
(
axis
=
1
)
)
Compute the proportion of missing values
(
"\n=== missing value ratio ==="
)
missing_ratio
=
df.
isna
(
)
.
mean
(
)
(
missing_ratio
)
Expected output:
=== 原始 DataFrame ===
name age score
0 Alice 25 85.0
1 Bob NaN 90.0
2 None 30 NaN
3 Diana 28 95.0
=== pd.notna() 检测结果 ===
name age score
0 True True True
1 True False True
2 False True False
3 True True True
=== 每列非缺失值数量 ===
name 3
age 3
score 3
=== 每行非缺失值数量 ===
0 3
1 2
2 1
3 3
=== 缺失值比例 ===
name 0.25
age 0.25
score 0.25
Code analysis:
pd.notna(df)returns a Boolean DataFrame with the same shape as the original, with each position indicating whether the value is non-missing.df.notna().sum()Count the non-missing values in each column.df.isna().mean()Calculate the proportion of missing values in each column to help assess data quality.
Tip:
pd.notna()The difference betweenpd.isna()are the exact opposite of each other.pd.notna()returns True wherepd.isna()returns False, and vice versa.
Common Pandas functions