Pandas DataFrame drop() Method

Rate this post


Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required libraries.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame drop()

The drop() method deletes rows/columns from a DataFrame by entering a label name(s) and an axis or entering the index/column name(s). To fully understand this method, feel free to watch this short tutorial:

The syntax for this method is as follows:

DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
labelsA single label or list of label(s) to delete from the DataFrame.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
indexRather than entering an index axis: axis=0, you could enter index=labels.
columnsRather than entering a column axis: axis=1, you could enter columns=labels.
levelIf multi-index, enter the appropriate level to delete.
inplaceIf False, create a copy of the DataFrame/Series. If zero (0) or index is selected, apply to each column. Default is None. If True, the original DataFrame/Series updates.
errorsIf set to ignore, the errors suppress.

For this example, Rivers Clothing has decided to drop Sweats from its clothing line. They need this clothing line removed. To perform this task, run the following code:

Code Example 1 (Simple Drop)

df = pd.DataFrame({'Tops':    [10, 12, 13],
                   'Tanks':   [11, 13, 14],
                   'Pants':   [21, 56, 94],
                   'Sweats':  [27, 21, 35]}, index=['Small', 'Medium', 'Large'])

result = df.drop('Sweats', axis=1)
  • Line [1] creates a DataFrame and saves it to df.
  • Line [2] drops the Sweats data from the DataFrame and saves it to the result variable.
  • Line [3] outputs the result to the terminal.



For this simple example, we drop a level from a DataFrame with a MultiIndex.

Code Example 2 (Multi-Level Drop)

cols = pd.MultiIndex.from_tuples([("A", "a"), ("C", "c"), ("E", "e")])
df   = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=cols)

Setting up the MultiIndex & DataFrame

  • Line [1] creates a MultiIndex from tuples and saves it to cols.
  • Line [2] creates a DataFrame with random data and sets the columns to cols.
  • Line [3] outputs the result to the terminal.
df.columns = df.columns.droplevel(0)
  • Line [4] uses droplevel() to drop the first column.
  • Line [5] outputs the result to the terminal.

💡Note: Replacing Line [4] with: df.columns = [col[0] for col in df.columns] accomplishes the same outcome using list comprehension.




After Drop


More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.