.drop()
Published May 12, 2022Updated May 23, 2022
Contribute to Docs
The .drop()
method returns a new DataFrame
object with rows or columns removed based on column or index names. The original DataFrame
object, used to call the method, remains unchanged.
Syntax
# Drop names from specified axis.
df = dataframevalue.drop(names, axis)
# Drop names from columns.
df = dataframevalue.drop(columns=names)
# Drop names from rows. (axis defaults to "0")
df = dataframevalue.drop(names)
dataframevalue
is the DataFrame with the source data.names
is a single label or a list of the items to drop.axis
is equal to0
for dropping rows and1
for dropping columns, it defaults to0
.columns
orindex
can be used to specify the labels to drop without usingaxis
.
DataFrame.drop()
has the following parameters:
Parameter Name | Data Type | Usage |
---|---|---|
labels |
single label or list | The labels to drop from columns or index as specified by axis . |
axis |
0/1 or ‘index’/‘columns’ | Specifies if labels refers to columns or indices. Defaults to 0 . |
index |
single label or list | Specifies index labels to drop. Ignores axis . |
columns |
single label or list | Specifies column labels to drop. Ignores axis . |
level |
index of level name | For MultiIndex dataframes, the level from which the label(s) will be removed. |
inplace |
bool | If True , alters the existing DataFrame rather than returning a new one. Defaults to False . |
errors |
‘ignore’/‘raise’ | Specifies if exception is raised for non-existent labels (‘raise’) or if those labels are ignored (‘ignore’). Default is ‘raise’. |
Example
In the following example, the .drop()
method is used in two separate instances:
import pandas as pdd = {'col 1' : [1,2,3,4], 'col 2' : ['A','B','C','D'], 'col 3' : [5,6,7,8], 'col 4' : ['E','F','G','H']}df = pd.DataFrame(data = d)print(f"Original df:\n {df}\n")first_drop = df.drop(columns='col 3')print(f"First drop():\n {first_drop}\n")second_drop = df.drop(2)print(f"Second drop():\n {second_drop}")
For the first .drop()
, the entire third column ('col 3'
) is removed. With the next .drop()
, the second row is removed altogether. These instances are reflected in the output below:
Original df:col 1 col 2 col 3 col 40 1 A 5 E1 2 B 6 F2 3 C 7 G3 4 D 8 HAfter first drop:col 1 col 2 col 40 1 A E1 2 B F2 3 C G3 4 D HAfter second drop:col 1 col 2 col 3 col 40 1 A 5 E1 2 B 6 F3 4 D 8 H
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pandas on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours