Pandas DataFrame Notna Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the notna() method in the Pandas library in Python. The notna() method is used to detect existing values in a DataFrame and returns a DataFrame consisting of boolean values for each element that indicates whether the element is not an NA value.

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/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") 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/variables_data_types -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} python/lists -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} python/tuples -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} python/importing_modules -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} python/data_collections -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} python/numerical_computing -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} python/data_analysis -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} python/build_in_functions -.-> lab-68680{{"`Pandas DataFrame Notna Method`"}} end

Import the necessary libraries

First, we need to import the pandas library using the alias pd and the numpy library using the alias np. These libraries are used for data manipulation and numerical operations respectively.

import pandas as pd
import numpy as np

Create a DataFrame

Next, we will create a DataFrame with some sample data to demonstrate the usage of the notna() method. We will use the pd.DataFrame() function to create 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'))

Display the DataFrame

Let's print the DataFrame to see its contents and structure.

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

Use the notna() method

Now, we will use the notna() method on the DataFrame to detect the existing values. The notna() method returns a DataFrame with the same shape as the original DataFrame, where each element is a boolean value indicating whether the corresponding element is not an NA value.

notna_df = df.notna()
print("The result of the notna() method is:")
print(notna_df)

Display the result

Let's print the resulting DataFrame to see the boolean values indicating the existing values.

print(notna_df)

Summary

In this lab, we learned how to use the notna() method in the Pandas library to detect existing values in a DataFrame. This method is useful for data cleaning and analysis tasks where we need to identify missing or null values. By using the notna() method, we can quickly obtain a DataFrame of boolean values indicating the presence or absence of existing values.

Other Python Tutorials you may like