Check if Values are NA/NaN in DataFrame
To check if values in DataFrame are NA or not in Pandas, call isna() method on this DataFrame. The method returns a DataFrame mask with shape as that of original and type of boolean, with True for NA values such as None or numpy.NaN and False for other values.
In this tutorial, we will learn the syntax of DataFrame.isna() method and how to use this method to check if values in DataFrame are NA or not.
Please note that empty strings or numpy.inf are not considered NA values (unless pandas.options.mode.use_inf_as_na = True).
Syntax
The syntax of DataFrame.isna() method is
DataFrame.isna()
This method does not have any parameters.
Return Value
DataFrame.
Examples
Check if Values are NA in DataFrame
In the following program, we take a DataFrame with some of its values as np.nan and None. When we call isna() on this DataFrame, it should return a DataFrame mask with True for the NA values and False for others.
Example.py
import pandas as pd
import numpy as np
data = {'col_1': [np.nan, 15, None, 25], 'col_2': [50, None, 60, 65]}
df = pd.DataFrame(data)
result = df.isna()
print(result)
Output
col_1 col_2
0 True False
1 False True
2 True False
3 False False
DataFrame with Empty Strings and np.inf
Now, let us take a DataFrame with some of the values as empty Strings and np.inf. The values must translate to False by isna() method.
Example.py
import pandas as pd
import numpy as np
data = {'col_1': [np.nan, "", None, np.inf], 'col_2': [50, None, 60, 65]}
df = pd.DataFrame(data)
result = df.isna()
print(result)
Output
col_1 col_2
0 True False
1 False True
2 True False
3 False False
Conclusion
In this Pandas Tutorial, we learned how to check if values in DataFrame is NA using pandas DataFrame.isna() method.