Pandas DataFrame Pop Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will be exploring the pop() method in the Python Pandas library. The pop() method is used to delete or drop a specified item in a DataFrame and returns the item. If the specified item is not found, the method raises a KeyError.

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 pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") 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 pandas/select_columns -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} python/lists -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} python/tuples -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} python/dictionaries -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} python/importing_modules -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} python/numerical_computing -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} python/data_analysis -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} python/build_in_functions -.-> lab-68696{{"`Pandas DataFrame Pop Method`"}} end

Importing the pandas library

First, we need to import the pandas library in order to use the pop() method.

import pandas as pd

Creating a DataFrame

Next, we will create a DataFrame object using the DataFrame() constructor. We will pass a dictionary containing the column labels and their corresponding values.

df = pd.DataFrame({'Name': ['Pooja', 'Sindu', 'Renuka'], 'Age': [18, 25, 20], 'Height': [145, 155, 165], 'Weight': [45, 55, 65]})

Dropping an item from the DataFrame

Now, we can use the pop() method to drop a specified item from the DataFrame. The method takes the label of the column to be popped as the parameter.

df.pop('Age')

Printing the modified DataFrame

Finally, we can print the modified DataFrame to see the changes.

print(df)

Here is the complete code:

import pandas as pd

df = pd.DataFrame({'Name': ['Pooja', 'Sindu', 'Renuka'], 'Age': [18, 25, 20], 'Height': [145, 155, 165], 'Weight': [45, 55, 65]})

df.pop('Age')

print(df)

Summary

In this lab, we learned how to use the pop() method in the Python Pandas library to delete or drop a specified item in a DataFrame. We also learned how to handle a KeyError if the specified item is not found in the DataFrame. The pop() method can be a useful tool for manipulating and modifying DataFrames in Python.

Other Python Tutorials you may like