search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | drop method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Data Selection and Renaming
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas's DataFrame.drop(~) method returns a DataFrame with the specified columns/rows removed.

Parameters

1. labels | string or list<string> | optional

The name(s) of the index or column to remove.

2. axis | int or string | optional

Whether to remove columns or rows:

Axis

Meaning

Removes rows

0 or "index"

Removes columns

1 or "columns"

By default, axis=0.

3. indexlink | string or list<string> | optional

The name(s) of the index to remove. This is equivalent to specifying parameters axis=0 and labels.

4. columnslink | string or list<string> | optional

The name(s) of the columns you want to remove. This is equivalent to specifying parameters axis=1 and labels.

5. inplacelink | boolean | optional

  • If True, then the method will directly modify the source DataFrame instead of creating a new DataFrame.

  • If False, then a new DataFrame will be created and returned.

By default, inplace=False.

NOTE

For clarity, specify the parameters index or columns instead of specifying labels and axis - the code is shorter and clearer.

Return Value

A DataFrame with the specified columns/rows removed. If inplace=True, then the return type is None since the source DataFrame is directly modified.

Examples

Dropping columns

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6]})
df
   A  B  C
0  1  3  5
1  2  4  6

Dropping a single column

To delete column A only:

df.drop(columns="A")
   B  C
0  3  5
1  4  6

This is equivalent to the following code:

df.drop(labels="A", axis=1)   # This is not the preferred way since the intention is unclear.

Note that since inplace=False, the original df is left intact.

Dropping multiple columns

To delete columns A and B:

df.drop(columns=["A","B"])
   C
0  5
1  6

This is equivalent to the following code:

df.drop(labels=["A","B"], axis=1)    # This is not the preferred way since the intention is unclear.
   C
0  5
1  6

Dropping rows

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2,3], "B":[4,5,6], "C":[7,8,9]})
df
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

Dropping a single row

To drop the second row (i.e. row with index=1):

df.drop(index=1)
   A  B  C
0  1  4  7
2  3  6  9

This is equivalent to the following code:

df.drop(labels=[1], axis=0)    # This is not the preferred way.
   A  B  C
0  1  4  7
2  3  6  9

Dropping multiple rows

To drop the first two rows (i.e. rows with index=0 and index=1):

df.drop(index=[0, 1])
   A  B  C
2  3  6  9

This is equivalent to the following code:

df.drop(labels=[0,1], axis=0)    # This is not the preferred way.
   A  B  C
2  3  6  9

Dropping in-place

To drop row(s) or column(s) in-place, we need to set inplace=True. What this does is that the drop(~) method will directly modify the source DataFrame instead of creating and returning a new DataFrame.

As an example, consider the following DataFrame:

df = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6]})
df
   A  B  C
0  1  3  5
1  2  4  6

We then drop columns A and B with inplace=True:

df.drop(columns=["A", "B"], inplace=True)
df
   C
0  5
1  6

As shown in the output, the source DataFrame has been modified.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!