search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

Pandas DataFrame | drop method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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

0 or "index"

Removes rows

1 or "columns"

Removes 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.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!