How to use Pandas drop() function in Python [Helpful Tutorial]

This Python tutorial is all about the Python Pandas drop() function. We will see how to use the Pandas drop() function in Python. Also, we will cover these topics:

In this tutorial, we will learn about how to use drop in pandas. Drop is a major function used in data science & Machine Learning to clean the dataset. Also, we will cover these topics

  • pandas drop syntax
  • pandas drop column
    • pandas drop column by index
    • pandas drop columns with condition
    • pandas drop column if exists
    • pandas drop column with nan
    • pandas drop columns with all zeros
    • pandas drop column while reading CSV
    • pandas drop column with no name
    • pandas drop columns except
    • pandas drop non numeric columns
  • pandas drop rows
    • pandas drop rows with condition
    • pandas drop rows with nan in column
    • pandas drop rows with nan+pandas drop rows with nan in specific column
    • pandas drop rows with condition string
    • pandas drop rows with value in any column
    • pandas drop range of rows
    • pandas drop rows with zero in column
    • pandas drop header row
    • pandas drop non integer rows
    • pandas drop non numeric rows
    • pandas drop blank rows
    • pandas drop missing rows

Pandas drop() function

The Pandas drop() function in Python is used to drop 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.

New to Python Pandas? Check out an article on Pandas in Python.

Pandas drop syntax

Below is the Pandas drop() function syntax.

DataFrame.drop(
    labels=None, 
    axis=0, 
    index=None, 
    columns=None, 
    level=None, 
    inplace=False, 
    errors='raise'
)
OptionsExplanation
labelsSingle label or list-like
Index or Column labels to drop.
axisthe drop will remove provided axis, the axis can be 0 or 1.
axis = 0 refers to rows or index (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 error and only existing labels are dropped
if raised then it will show the error message & won’t allow dropping the data.

Check out, Groupby in Python Pandas.

Pandas drop column

Let us see how to use Pandas drop column.

Pandas drop column by index

  • Index refers to rows or axis=0. To drop column by index we need to pass the value of the index.
  • In every dataset, the first column on the left has a serial number, part number, or something that is unique every time. Rows on that column are called index.
  • User can create their own indexes as well using the keyword index followed by a list of labels

You may also like, Crosstab in Python Pandas.

Pandas drop columns with condition

In this section, we will learn how to drop columns with condition in pandas.

Pandas drop column if exists

In this section, we will learn how to drop column if exists.

Pandas drop column with nan

  • In this section, we will learn how to drop columns with nan. Here nan means missing values. To drop the missing values we will use dropna() function. This function is created for the purpose of deleting or removing missing values.
  • In the below example, you will notice that columns that have missing values will be removed. BMI column has missing values so it will be removed.

Also, you may like to read, Missing Data in Pandas in Python.

Pandas drop columns with all zeros

In this section, we will learn how to delete columns with all zeros in Python pandas using the drop() function.

Pandas drop column while reading CSV

  • In this section, we will learn how to drop column(s) while reading the CSV file. To do so we pass the drop command with the read_csv command.
  • In the below implementation, you can notice that we have removed ‘bmi‘ & ‘stroke‘ columns while reading the CSV file.

Read, Python Pandas CSV Tutorial.

Pandas drop column with no name

  • no name column is automatically created when the file is exported and appears with the name Unnamed: 0
  • to avoid the creation of no name or Unnamed: 0 columns set index=False
  • df.to_csv('exported_file.csv', index=False')
  • But in case the file already has no name or Unnamed: 0 columns and you want to remove them then follow the implementation below.

Pandas drop columns except

  • In this section, we will learn how to add exceptions while dropping columns.
  • df.columns.difference() is used to perform exception
  • all the columns passed in difference() won’t be dropped

Check out, How to read video frames in Python.

Pandas drop non numeric columns

In this section, we will learn to drop non numeric columns

Pandas drop rows

In this section, we will learn how to drop rows in pandas

Pandas drop rows with condition

In this section, we will learn how to drop rows with condition.

Also, you may like, Python String Functions.

Pandas drop rows with nan in column

  • In this section, we will learn how to remove the row with nan or missing values.
  • While cleaning the dataset at times we encounter a situation wherein so many missing values are displayed. In that case, Data Engineer may take a decision to drop missing values.
  • dropna() function is used to drop all the missing values.

Pandas drop rows with nan

  • In this section, we will learn how to remove the row with nan or missing values.
  • While cleaning the dataset at times we encounter a situation wherein so many missing values are displayed. In that case, Data Engineer may take a decision to drop missing values.
  • dropna() function is used to drop all the missing values.

Check out, How to create a list in Python.

Pandas drop rows with nan in specific column

  • In this section, we will learn how to drop rows with nan or missing values in the specified column.
  • In our dataset ‘bmi’ column has missing values so we will be performing

Pandas drop rows with condition string

In this section, we will learn how to drop rows with condition string

Pandas drop rows with value in any column

In this section, we will learn how to drop rows with value in any column

Also, you may like to read, How to convert an integer to string in python?

Pandas drop range of rows

  • In this section, we will learn how to drop range of rows in python pandas.
  • Using python slicing operation we can drop rows in a range

Pandas drop rows with zero in column

  • In this section, we will learn how to drop rows with zero in a column using pandas drop
  • As per our dataset, we will be removing all the rows with 0 values in the hypertension column.

Pandas drop header row

In this section, we will learn how to drop the header rows. In our demonstration we will create the header row then we will drop it.

Check out, Python naming conventions.

Pandas drop non-integer rows

In this section, we will learn how to drop non integer rows.

Pandas drop non numeric rows

In this section, we will learn how to drop non numeric rows.

Pandas drop blank rows

In this section, we will learn how to remove blank rows in pandas. Blank rows are represented with nan in pandas. So ultimately we will be removing nan or missing values.

Read, How to split a string using regex in python?

Pandas drop missing rows

  • In this section, we will learn how to remove the row with nan or missing values.
  • While cleaning the dataset at times we encounter a situation wherein so many missing values are displayed. In that case, Data Engineer may take a decision to drop missing values.
  • dropna() function is used to drop all the missing values.

Check out the below Python tutorials:

In this tutorial we have learned how to drop data in python pandas also we have covered these topics.

  • pandas drop syntax
  • pandas drop column
    • pandas drop column by index
    • pandas drop columns with condition
    • pandas drop column if exists
    • pandas drop column with nan
    • pandas drop columns with all zeros
    • pandas drop column while reading CSV
    • pandas drop column with no name
    • pandas drop columns except
    • pandas drop non numeric columns
  • pandas drop rowspandas drop rows with condition
    • pandas drop rows with nan in column
    • pandas drop rows with nan+pandas drop rows with nan in specific column
    • pandas drop rows with condition string
    • pandas drop rows with value in any column
    • pandas drop range of rows
    • pandas drop rows with zero in column
    • pandas drop header row
    • pandas drop non integer rows
    • pandas drop non numeric rows
    • pandas drop blank rows
    • pandas drop missing rows