• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 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. NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. In pandas handling missing data is very important before you process it.

None/NaN values are one of the major problems in Data Analysis hence before we process either you need to remove columns that have NaN values or replace NaN with empty for String or replace NaN with zero for numeric columns based on your need. 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.

1. Quick Example of isna() Function

Below are quick examples of DataFrame.isna() function.


# Below are the quick examples

# 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)

2. Syntax of DataFrame.isna() function.

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


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

2.1 Parameters

This function doesn’t have any parameters.

2.2 Return Value

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

3. 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],
    '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

4. 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

5. 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

6. 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

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

You can also get or find the count of NaN values of all columns in a Pandas DataFrame using the isna() function with sum() function. df.isna().sum() syntax returns the number of NaN values in all columns of a pandas DataFrame in Python.


# 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

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

We can also count the NaN values of all rows using the isna() function. By default sum() function adds all column values whereas, to get the rows to count we have to pass the axis param as '1' into the sum() function, and it will add all row values.

If you want drop rows with NaN values in a DataFrame, you can drop using drop() function


# 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

9. 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!!

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.