Identify present values
Build a validity mask before filtering, validation, or conditional updates.
Pandas · Module
Detects which DataFrame elements are non-missing. It returns a same-sized boolean DataFrame, with True for present values and False for recognized NA values.
Call DataFrame.notnull() to produce a same-shaped boolean DataFrame: each non-missing element becomes True, while recognized missing values become False. It is an alias of DataFrame.notna() and preserves the DataFrame’s labels and dimensions.
DataFrame.notnull()DataFrame: A boolean mask with the same shape, whose elements indicate whether corresponding values are not NA.
True in the returned mask.None and numpy.NaN, become False.DataFrame.notna() and has equivalent behavior.True.import numpy as np
import pandas as pd
df = pd.DataFrame({
"age": [5, 6, np.nan],
"name": ["Alfred", "Batman", ""],
"toy": [None, "Batmobile", "Joker"],
})
mask = df.notnull()
print(mask)
print(df[mask["age"]])Creates a boolean mask for existing values and uses one column of that mask to keep rows with a known age.
Build a validity mask before filtering, validation, or conditional updates.
Select records whose required field is populated using boolean indexing.
Inspect missingness patterns across columns while retaining the original DataFrame layout.
DataFrame.notnull() return?It returns a boolean DataFrame with the same shape and labels as the input. Each value is True when it is not recognized as missing and False when it is NA.
Follow-up: How would you retain only rows with a present value in one column? Use the resulting boolean Series as a row mask, for example df[df["column"].notnull()].
notnull() relate to notna()?DataFrame.notnull() is an alias for DataFrame.notna(), so both methods perform the same missing-value test. The method operates element by element across the DataFrame.
Follow-up: What is the complementary operation? DataFrame.isna() identifies missing values, making its boolean result the inverse test for this operation.
Values such as None and numpy.NaN are mapped to False, while existing values are mapped to True. The output remains a DataFrame rather than collapsing to one overall truth value.
Follow-up: How can the mask be used for selection? Combine it with boolean indexing, or apply reductions such as any() or all() when a row- or column-level result is needed.
DataFrame.notnaDataFrame.isnaDataFrame.isnull