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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/variables_data_types -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/lists -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/tuples -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/dictionaries -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/importing_modules -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/data_collections -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/numerical_computing -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/data_analysis -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} python/build_in_functions -.-> lab-68640{{"`Pandas DataFrame Isna Method`"}} end

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