How to drop all Rows in a Pandas DataFrame in Python

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. How to drop all Rows in a Pandas DataFrame in Python
  2. Drop all Rows in a DataFrame using DataFrame.iloc
  3. Drop all rows in a DataFrame by instantiating a new DataFrame with the same columns

# How to drop all Rows in a Pandas DataFrame in Python

To drop all rows in a Pandas DataFrame:

  1. Call the drop() method on the DataFrame
  2. Pass the DataFrame's index as the first parameter.
  3. Set the inplace parameter to True.
main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], }) print(df) print('-' * 50) df.drop(df.index, inplace=True) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary 0 Alice 175.1 1 Bobby 180.2 2 Carl 190.3 -------------------------------------------------- Empty DataFrame Columns: [name, salary] Index: []

drop all rows in pandas dataframe

We used the DataFrame.drop method to drop all rows from a DataFrame.

The first argument the method takes is the column labels that you want to drop.

The method can be called with a single label or a list-like object of column labels.

We set the argument to DataFrame.index in order to drop all rows from the DataFrame.

The DataFrame.index method returns the index (row labels) of the DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], }) print(df) print('-' * 50) # 👇️ [0, 1, 2] print(df.index.tolist())
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary 0 Alice 175.1 1 Bobby 180.2 2 Carl 190.3 -------------------------------------------------- [0, 1, 2]

using df index to select row labels

Notice that we also set the inplace argument to True when calling DataFrame.drop().

main.py
df.drop(df.index, inplace=True)

When the inplace argument is set to True, the DataFrame rows are dropped in place and None is returned.

If you only want to drop specific rows from the DataFrame, set the index argument to a list containing the index labels you want to drop.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [5, 10, 15] }) print(df) print('-' * 50) indexes_to_drop = [0, 2] df.drop(index=indexes_to_drop, inplace=True) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary experience 0 Alice 175.1 5 1 Bobby 180.2 10 2 Carl 190.3 15 -------------------------------------------------- name salary experience 1 Bobby 180.2 10

drop specific rows in pandas dataframe

# Drop all Rows in a DataFrame using DataFrame.iloc

You can also use the iloc position-based indexer to drop all rows in a DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], }) print(df) print('-' * 50) df = df.iloc[0:0] print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary 0 Alice 175.1 1 Bobby 180.2 2 Carl 190.3 -------------------------------------------------- Empty DataFrame Columns: [name, salary] Index: []

drop all rows in dataframe using df iloc

We used the df.iloc position-based indexer to select an empty slice of the rows.

main.py
df = df.iloc[0:0]

You can also shorten this a little.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], }) print(df) print('-' * 50) df = df[0:0] print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary 0 Alice 175.1 1 Bobby 180.2 2 Carl 190.3 -------------------------------------------------- Empty DataFrame Columns: [name, salary] Index: []

drop all rows in dataframe using slicing

# Drop all rows in a DataFrame by instantiating a new DataFrame with the same columns

You can also drop all rows in a DataFrame by using the pandas.DataFrame constructor to instantiate a new DataFrame with the same columns.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], }) print(df) print('-' * 50) df = pd.DataFrame(columns=df.columns) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary 0 Alice 175.1 1 Bobby 180.2 2 Carl 190.3 -------------------------------------------------- Empty DataFrame Columns: [name, salary] Index: []

drop all rows in pandas dataframe using pd dataframe

The pandas.DataFrame constructor takes a columns argument.

We set the argument to the columns of the existing DataFrame to create a new DataFrame with the same columns, without any rows.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], }) # 👇️ Index(['name', 'salary'], dtype='object') print(df.columns)
The code for this article is available on GitHub

If you don't want to keep the columns around, pass None to the pandas.DataFrame() constructor.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], }) print(df) print('-' * 50) df = pd.DataFrame(None) print(df)
shell
name salary 0 Alice 175.1 1 Bobby 180.2 2 Carl 190.3 -------------------------------------------------- Empty DataFrame Columns: [] Index: []

drop all rows without keeping columns

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.