• Post author:
  • Post category:Pandas
  • Post last modified:May 9, 2024
  • Reading time:10 mins read
You are currently viewing Pandas DataFrame isna() Function

Pandas DataFrame.isna() function is used to check the missing values in a given DataFrame. It returns a same-sized DataFrame object where the values are replaced with a Boolean value True for every NAN (not-a-number) value, and otherwise False.This is the best way to check missing values in a DataFrame using the isna() function. Moreover, using isna() function along with sum() we can count the NaN values in a DataFrame.

Advertisements

In this article, I will explain the isna() function, its syntax, parameters, and usage of how the isna() function in Pandas DataFrame is used to detect missing values (NaN in numeric arrays, None/NaN in object arrays). It returns a boolean DataFrame of the same shape as the original DataFrame, with True for missing values and False for non-missing values.

Key Points –

  • Pandas DataFrame isna() function checks for missing or NaN values in a DataFrame.
  • It returns a boolean DataFrame of the same shape as the input DataFrame, where True indicates a missing value and False indicates a non-missing value.
  • It can handle missing values represented as NaN in numeric arrays and None/NaN in object arrays.
  • The boolean DataFrame returned by isna() has the same shape as the original DataFrame, making it easy to integrate into data analysis pipelines

Quick Example of isna() Function

If you are in a hurry, below are some quick examples of how of DataFrame.isna() function.


# Quick example of isna() function

# Example 1: Detect NaN values 
# Using df.isna()
df1 = df.isna()

# Example 2: Apply isna() function 
# To specific Column
df1 = df['Fee'].isna()

# Example 3: Count NaN values of DataFrame
nan_count = df.isna().sum().sum()

# Example 4: Count the NaN values in column-wise 
nan_count = df.isna().sum()

# Example 5: Count the NaN values in row-wise
nan_count = df.isna().sum(axis = 1)

Syntax of DataFrame.isna() Function

Following is the syntax of Pandas DataFrame.isna() function.


# Syntax of df.isna()
DataFrame.isna()

Parameters

This function doesn’t have any parameters.

Return Value

It returns a boolean DataFrame for DataFrame having missing values otherwise, it returns boolean Series for a specific column.

Pandas DataFrame.isna() Usage

Pandas DataFrame.isna() function is used to check whether missing values exist in a given DataFrame or not. Either you can apply this function to Pandas DataFrame that returns a boolean DataFrame or apply it to a specific column of DataFrame to return boolean Pandas Series.

Now, let’s create a DataFrame with a few rows and columns using Python Dictionary. Our DataFrame contains the column names CoursesFeeDuration, and Discount and includes some NaN values on a string and integer columns.


# Create pandas DataFrame
import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark", np.nan, "PySpark", np.nan, "Hadoop"],
    'Fee' :[np.nan, 20000, np.nan, 25000, np.nan],
    'Duration':[np.nan,'40days','35days', np.nan, np.nan],
    'Discount':[np.nan, 1000, np.nan, np.nan, 1500]
               }
df = pd.DataFrame(technologies, index = ['r1', 'r2', 'r3', 'r4', 'r5'])
print(df)

Yields below output. Note that in Pandas nan can be defined by using NumPy np.nan.

Pandas isna
Pandas DataFrame

Apply isna() Function to Whole DataFrame

Apply isna() function to a given DataFrame having some missing values then, it will check whether it contains missing values. If it contains it is replaced with True otherwise False.


# Detect NaN values using df.isna()
df1 = df.isna()
print(df1)

Yields below output.

Pandas isna
Pandas DataFrame with boolean values

Apply isna() Function to Specific Column

Apply isna() function to the specific column of the given DataFrame, it checks the NaN values and returns the boolean values in the form of Series. For example,


# Apply isna() Function to specific Column
df1 = df['Fee'].isna()
print(df1)

# Output:
# r1     True
# r2    False
# r3     True
# r4    False
# r5     True
# Name: Fee, dtype: boo

Count NaN Values using Pandas isna() along with sum()

So far, we have learned how to check the missing values using the isna() function now, we will learn how to count the NaN values in a DataFrame/row-wise/column-wise using isna() function along with the sum() function. Let’s apply these functions and count the NaN values.

Let’s count the total number of NaN values in the whole DataFrame, using df.isna().sum().sum(), this syntax returns the total number of NaN values in the entire DataFrame.


# Count NaN values of DataFrame
nan_count = df.isna().sum().sum()
print(nan_count )

# Output:
# 11

Count NaN Values in Pandas Column-Wise using isna()

To count the NaN values column-wise in a Pandas DataFrame using the isna() function, you can simply chain it with the sum() function along the desired axis, which is axis=0 for column-wise operations.


# Count the NaN values column-wise of DataFrame
nan_count = df.isna().sum(axis=0)
print(nan_count)

# Count the NaN values column-wise of DataFrame
nan_count = df.isna().sum()
print(nan_count )

# Output:
# Courses     2
# Fee         3
# Duration    3
# Discount    3
# dtype: int64

Count NaN Values in Pandas Row-Wise using isna()

To count NaN values row-wise in a Pandas DataFrame using the isna() function, you can utilize the sum() function along with axis=1.


# Count the NaN values in row-wise
nan_count = df.isna().sum(axis = 1)
print(nan_count)

# Output:
# r1    3
# r2    1
# r3    2
# r4    3
# r5    2
# dtype: int64

Conclusion

In this article, I have explained the Pandas DataFrame.isna() function and used this function to check whether the missing values are present in the DataFrame or not with examples.

Happy learning!!