Pandas DataFrame Isna Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the DataFrame.isna() method in Pandas. The isna() method is used to detect missing values in a pandas DataFrame. It returns a DataFrame of boolean values, where each element indicates whether it is a null value or not. The isna() method does not consider empty strings or special values like numpy.inf as null values.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Creating a DataFrame

First, let's create a DataFrame with some missing values using the DataFrame() function from the pandas library. We'll import the necessary libraries and create the DataFrame with columns 'a', 'b', 'c', and 'd'.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np

#creating the DataFrame
df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),
                   (np.nan, 2.0, np.nan, np.nan),
                   (2.0, 3.0, np.nan, 9.0)],
                  columns=list('abcd'))

print("------The DataFrame is----------")
print(df)

Detecting Missing Values

Next, we'll use the isna() method to detect missing values in the DataFrame. We'll print the result to see which elements are null values.

print("---------------------------------")
print(df.isna())

Evaluating the Results

By running the code, we can see that the isna() method returned a DataFrame consisting of boolean values for each element in the original DataFrame. False indicates that the element is not a null value, while True indicates that the element is a null value.

Considering Empty Strings

In the previous example, the isna() method did not consider empty strings as null values. Let's create another DataFrame and check if the isna() method still behaves the same.

#creating another DataFrame
df = pd.DataFrame({'a': [0, 1, ''], 'b': ['', None, 3]})

print("------The DataFrame is----------")
print(df)

Detecting Missing Values Again

Now, let's use the isna() method on the new DataFrame to detect the missing values.

print("---------------------------------")
print(df.isna())

Summary

In this lab, we learned how to use the DataFrame.isna() method in Pandas to detect missing values in a DataFrame. We created a DataFrame with missing values, used the isna() method to detect those missing values, and observed the results. Additionally, we saw that the isna() method does not consider empty strings as null values. This method is useful for handling missing data in pandas DataFrames.


Please let me know if you need any further assistance.

Other Python Tutorials you may like