Pandas Dataframe drop() function in Python [4 Examples]

Do you come across the drop function in Python? Let me introduce you to the Pandas dataframe drop() function in Python.

In this Python Pandas tutorial, I will explain what the Pandas dataframe drop() function in Python is, its syntax, the parameters required, and its return values. I will also explain how to use the drop function in Python with the help of some examples.

Pandas DataFrame drop() Method

The Pandas drop() function in Python drops specified labels from rows and columns. Drop is a major function used in data science & Machine Learning to clean the dataset.

Pandas Drop() function removes specified labels from rows or columns. When using a multi-index, labels on different levels can be removed by specifying the level.

The drop() function removes specified rows or columns from a Pandas DataFrame or Series.

Syntax of Pandas Dataframe drop() function in Python

Here is the syntax for the Pandas drop() function in Python.

DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

List of the parameters required in the Pandas drop in Python:

OptionsExplanation
labelsSingle-label or list-like
Index or Column labels to drop.
axisthe drop will remove the provided axis; the axis can be 0 or 1.
axis = 0 refers to rows or indexes (verticals)
axis = 1 refers to columns (horizontals)
by default, axis = 0
indexsingle label or list-like.
the index is the row (verticals) & is equivalent to axis=0
columnsSingle label or list-like.
the columns are horizontals in the tabular view & are denoted with axis=1.
levelint or level name, optional
For MultiIndex, the level from which the labels will be removed.
inplaceaccepts bool (True or False), default is False.
Inplace makes changes then & there. don’t need to assign a variable.
errorsthe error can be ‘ignored‘ or ‘raised.’ default is ‘raised.’
if ignored, suppress the error, and only existing labels are dropped
if raised, it will show the error message & won’t allow dropping the data.

The drop() function in the Python pandas library is useful for removing specified rows or columns from a DataFrame or Series. The function takes in several parameters, including the labels to drop, the axis (i.e., rows or columns), and whether or not to modify the original DataFrame in place.

READ:  Python Scipy Stats Poisson - Useful Guide

With the Pandas dataframe drop() method, we can easily manipulate the structure of our data by removing unnecessary rows or columns. We can also chain multiple drop() functions in Python to remove multiple rows or columns simultaneously.

It’s important to note that the Python drop() function in Pandas with inplace=True modifies the original DataFrame in place and does not return a new DataFrame object. This can be useful to save memory or avoid creating unnecessary copies of our data.

Examples of Pandas drop() function in Python

Let’s look at some examples to understand better how to use the drop() function in Pandas in Python.

How to use the drop function in Python for dropping rows

Suppose we have the following DataFrame in Python:

import pandas as pd

employee = {
    'name': ['Alice', 'Bob', 'Charlie', 'David'],
    'age': [25, 30, 35, 40],
    'gender': ['F', 'M', 'M', 'M']
}
employee_df = pd.DataFrame(employee)
print(employee_df)

Output:

       name  age gender
0     Alice   25      F
1       Bob   30      M
2   Charlie   35      M
3     David   40      M
Pandas Dataframe drop() function in Python

To drop the row with index 1 (i.e., the row with ‘Bob’), we can use the following code:

employee_df = employee_df.drop(1)
print(employee_df)

Output:

       name  age gender
0     Alice   25      F
2   Charlie   35      M
3     David   40      M

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

drop function in python pandas

We can also drop multiple rows at once by specifying a list of indices to drop in Pandas Python:

import pandas as pd

employee = {
    'name': ['Alice', 'Bob', 'Charlie', 'David'],
    'age': [25, 30, 35, 40],
    'gender': ['F', 'M', 'M', 'M']
}
employee_df = pd.DataFrame(employee)
print("Original DataFrame:\n", employee_df)

# Drop rows with index 0 and 2
employee_df = employee_df.drop([0, 2])
print("\nModified DataFrame:\n", employee_df)

Output:

Original DataFrame:
       name  age gender
0    Alice   25      F
1      Bob   30      M
2  Charlie   35      M
3    David   40      M

Modified DataFrame:
     name  age gender
1    Bob   30      M
3  David   40      M

Below is a screenshot that reveals the output after the code has been implemented in the Pycharm editor.

pandas drop method in Python

Dataframe drop column in Pandas

To drop a column in Python Pandas, we can set axis=1. Here is an example:

employee_df = employee_df.drop('gender', axis=1)
print(employee_df)

Output:

     name  age
0   Alice   25
1     Bob   30
2  Charlie  35
3   David   40

Following implementing the code in the Pycharm editor, the screenshot below has been provided.

Delete rows/columns from DataFrame using Pandas.drop() function in Python

Again, we can drop multiple columns using Pandas at once by specifying a list of column names in the Pandas drop() method in Python:

employee_df = employee_df.drop(['name', 'age'], axis=1)
print(employee_df)

Output:

  gender
0      F
1      M
2      M
3      M

Below is a screenshot showcasing the output captured after the code was executed in the Pycharm editor.

drop function in pandas dataframe in Python

Drop method in Python with the inplace argument

By default, the drop() function in Python does not modify the original Pandas DataFrame. Instead, it returns a new DataFrame with the specified rows or columns dropped. If we want to modify the original DataFrame in place, we can set the ‘inplace=True‘:

READ:  How to Create a Snake game in Python using Turtle

Here is an instance of the Pandas dataframe drop() function in Python

import pandas as pd

employee = {
    'name': ['Alice', 'Bob', 'Charlie', 'David'],
    'age': [25, 30, 35, 40],
    'gender': ['F', 'M', 'M', 'M']
}
employee_df = pd.DataFrame(employee)
print("Original DataFrame:\n", employee_df)
employee_df.drop(1, inplace=True)
print("\nModified DataFrame:\n", employee_df)

Output:

Original DataFrame:
       name  age gender
0    Alice   25      F
1      Bob   30      M
2  Charlie   35      M
3    David   40      M

Modified DataFrame:
       name  age gender
0    Alice   25      F
2  Charlie   35      M
3    David   40      M

As you can see, the row with index 1 (‘Bob’) has been dropped from the original DataFrame inplace. It’s important to note that when using inplace=True, the function returns None and does not create a new DataFrame object.

After executing the code in Pycharm, one can see the output in the below screenshot.

drop function python pandas with inplace argument
Python pandas drop() function

Conclusion

The Pandas dataframe drop() function in Python is a powerful tool that can help us clean and manipulate our data. We can use this function to drop unnecessary rows or columns at a time from a dataset in Pandas using the drop() method in Python.

I hope you understand the drop() method from Pandas in Python in detail.

You may like the following Python Pandas tutorials: