Pandas is a two-dimensional data structure that allows you to store data in rows and columns format. It also provides a lot of API methods that can be used for easier data analysis. Two such methods are isna()
and isnull()
Both isna()
and isnull()
functions are used to find the missing values in the pandas dataframe.
isnull()
and isna()
literally does the same things. isnull()
is just an alias of the isna()
method as shown in pandas source code.
Missing values are used to denote the values which are null or do not have any actual values. You can use the pd.NaT
, np.NaN
or None
to denote the missing values in the dataframe.
Table of Contents
What is isna()
isna() is used to detect the missing values in the cells of the pandas dataframe.
It returns a dataframe of the same size with the values masked as True
for na
values and False
for non-NA values.
What is isnull()
isnull() is also used to identify or detect the missing values in the dataframe. It is just an alias for isna()
method.
It also returns a dataframe of the same size where the values such as None
or Np.NaN
are masked as True
and other values are masked as False
.
Why Two Methods in Different Name
Pandas dataframe are based on R dataframes. In R, the na
values and null
values are different types. Hence, there are two different methods to check na
and null
. That’s why pandas have two method names.
On the other hand, in Python pandas is built on top of NumPy which doesn’t have na
or null
values. It uses Np.NaN
values to denote the missing values. Even None
values are considered as Np.NaN
.
You’ll see it in detail in the below examples.
Finding Missing Values
In this section, you’ll use the isna()
and the isnull()
method to find the missing values in the sample dataframe.
The dataframe contains all the different types of the missing values pd.naT
, None
, Np.NaN
.
Sample Dataframe
import pandas as pd
import numpy as np
data = {"Product_Name":["Mouse", "Monitor", "CPU", "Speakers","Headset"],
"Unit_Price":[200, 5000.235, 10000.550, 250.50, None],
"No_Of_Units":[5, 10, 20, 8, pd.NaT],
"Available_Quantity":[6,5,5, pd.NaT,np.NaN],
"Remarks":[np.NaN,pd.NaT,pd.NaT,pd.NaT,pd.NaT]
}
df = pd.DataFrame(data)
df
Dataframe Will Look Like
Product_Name | Unit_Price | No_Of_Units | Available_Quantity | Remarks | |
---|---|---|---|---|---|
0 | Mouse | 200.000 | 5 | 6 | NaT |
1 | Monitor | 5000.235 | 10 | 5 | NaT |
2 | CPU | 10000.550 | 20 | 5 | NaT |
3 | Speakers | 250.500 | 8 | NaT | NaT |
4 | Headset | NaN | NaT | NaN | NaT |
Detect Missing Values Using isna()
You can use the below snippet to find the missing values in the dataframe using isna()
.
The values None
, Np.NaN
and the pd.Nat
will be identified as missing values when you use the isna()
function.
Snippet
df.isna()
The cells that have True
denote that has missing values and the cells that have False
denote that have a valid value.
Dataframe Will Look Like
Product_Name | Unit_Price | No_Of_Units | Available_Quantity | Remarks | |
---|---|---|---|---|---|
0 | False | False | False | False | True |
1 | False | False | False | False | True |
2 | False | False | False | False | True |
3 | False | False | False | True | True |
4 | False | True | True | True | True |
Detect Missing Values Using isNull()
You can use the below snippet to find the missing values in the dataframe using isnull()
.
As the isnull()
method is just an alias of the isna()
method, it’ll also identify the values None
, Np.NaN
and the pd.Nat
as missing values.
Snippet
df.isnull()
The cells that have True
denote that have missing values and the cells that have False
denote that have a valid value.
Dataframe Will look like
Product_Name | Unit_Price | No_Of_Units | Available_Quantity | Remarks | |
---|---|---|---|---|---|
0 | False | False | False | False | True |
1 | False | False | False | False | True |
2 | False | False | False | False | True |
3 | False | False | False | True | True |
4 | False | True | True | True | True |
Which One to Use
You can use the isna()
method to identify the missing values. Because it is the original method implemented and isnull()
is just an alias that internally calls the isna()
method.
Conclusion
To summarize, you’ve learned the difference between isnull()
and isna()methods in the pandas dataframe. You’ve also learned which method needs to be used.
You May Also Like
Is ISNA and Isnull same?
Yes, both methods are used to detect the missing values. isnull() is just an alias of the isna() method and internally uses isna method.