Pandas DataFrame notnull() Method
DataFrame.notnull() function in pandas detect non-missing (non-NaN/None) values in a DataFrame. It returns a DataFrame of the same shape as the original, with boolean values indicating whether each element is not null. Example:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, 3],
'B': ['x', 'y', None]
})
print(df.notnull())
Output
A B 0 True True 1 False True 2 True False
Explanation: Returns a DataFrame of the same shape where True means the value is not null i.e., valid and False means the value is NaN or None.
Syntax
DataFrame.notnull()
Parameters: This method does not take any parameters.
Returns: A DataFrame of boolean values, with True where values are not null and False where values are NaN or None.
Examples
Example 1: In this example, we check which values in the DataFrame are not missing.
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': ['a', 'b', None]})
print(df.notnull())
Output
A B 0 True True 1 False True 2 True False
Explanation: This returns a DataFrame with True where values are not null and False where values are NaN or None.
Example 2: Filter rows where column 'A' is not null using boolean indexing.
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': ['a', 'b', None]})
print(df[df['A'].notnull()])
Output
A B 0 1.0 a 2 3.0 None
Explanation: Only rows where 'A' is not null are returned. The row with NaN in column 'A' is excluded.
Example 3: Compare notnull() with the inverse of isnull() to verify they are logical opposites.
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': ['a', 'b', None]})
print(df.notnull() == ~df.isnull())
Output
A B 0 True True 1 True True 2 True True
Explanation: notnull() returns the opposite of isnull() and the comparison confirms this by returning True for all elements.
Example 4: In this example, we check if all values in each column are not null using all().
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': ['a', 'b', None]})
print(df.notnull().all())
Output
A False B False dtype: bool
Explanation: False indicates that not all values in the columns are non-null both columns contain at least one missing value.
Example 5: In this example, we check if any value in each row is not null using any(axis=1).
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': ['a', 'b', None]})
print(df.notnull().any(axis=1))
Output
0 True 1 True 2 True dtype: bool
Explanation: True means that each row contains at least one non-null value, even if some values in the row are missing.
Related articles: