Python Pandas - Removing Rows from a DataFrame



Data cleaning is an essential step in preprocessing, and removing unwanted rows is a common operation in Pandas. A Pandas DataFrame is a two-dimensional data structure in Python that organizes data in a tabular format, consisting of rows and columns. It is widely used for data analysis and manipulation tasks, enabling efficient handling of large datasets.

Removing rows may be necessary for various reasons −

  • Removing the irrelevant data

  • Removing duplicate or missing values

  • Deleting specific rows based on conditions

Pandas provides multiple ways to remove rows efficiently. In this tutorial, we will learn about various techniques to remove/drop rows from a pandas DataFrame, including −

  • Using the .drop() method

  • Removing rows based on conditions

  • Dropping rows with index slicing

Dropping Rows using the drop() method

The pandas DataFrame.drop() method is used to remove a specific row from the pandas DataFrame. It can be used to drop rows by their label or position (integer-based index), and it returns a new DataFrame with the selected rows removed.

Example: Dropping DataFrame Rows by Index Values

Here is a basic example of deleting a row from a DataFrame object using the DataFrame.drop() method based on its index value.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3, 4, 5],'B': [4, 5, 6, 7, 8]}) # Display original DataFrame print("Original DataFrame:") print(df) # Drop the row with index 3 result = df.drop(3) # Display the result print("\nAfter dropping the row at index 3:") print(result)

Following is the output of the above code −

Original DataFrame:
A B
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
After dropping the row at index 3:
A B
0 1 4
1 2 5
2 3 6
4 5 8

Note: This method will raise a KeyError if the specified row label or index is not found in the index of the DataFrame. And this error can be suppressed by setting the errors parameter from raise to ignore.

Dropping Multiple Rows by Labels

By providing the list of multiple row labels to the drop() method, we can easily remove multiple rows at a time from a DataFame.

Example

Similar to the previous example the following one will delete the multiple rows from a DataFrame based on its row labels using the DataFrame.drop() method. Here we are specified list of row labels to the drop() method.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3, 4, 5],'B': [4, 5, 6, 7, 8], 'C': [9, 10, 11, 12, 13]}, index=['r1', 'r2', 'r3', 'r4', 'r5']) # Display original DataFrame print("Original DataFrame:") print(df) # Drop the rows by row-labels result = df.drop(['r1', 'r3']) # Display the result print("\nAfter dropping the rows:") print(result)

Following is the output of the above code −

Original DataFrame:
A B C
r1 1 4 9
r2 2 5 10
r3 3 6 11
r4 4 7 12
r5 5 8 13
After dropping the row:
A B C
r2 2 5 10
r4 4 7 12
r5 5 8 13

Removing Rows Based on a Condition

Rows can be removed based on a conditional expression, meaning that you can use a condition inside a selection brackets [] to filter the rows. This method is useful when filtering out rows that meet a specific condition, such as missing values or unwanted entries.

Example

This example demonstrates how to drop row or rows from a Pandas DataFrame based on a conditional statement specified inside the []. In this example row deletion done is based on a DataFrame on column value.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3, 4, 5],'B': [4, 5, 6, 7, 8], 'C': [90, 0, 11, 12, 13]}, index=['r1', 'r2', 'r3', 'r4', 'r5']) # Display original DataFrame print("Original DataFrame:") print(df) # Dropping rows where column 'C' contains 0 result = df[df["C"] != 0] # Display the result print("\nAfter dropping the row where 'C' has 0:") print(result)

Following is the output of the above code −

Original DataFrame:
A B C
r1 1 4 90
r2 2 5 0
r3 3 6 11
r4 4 7 12
r5 5 8 13
After dropping the row where 'C' has 0:
A B C
r1 1 4 90
r3 3 6 11
r4 4 7 12
r5 5 8 13

Removing Rows using Index Slicing

This is the another approach of removing or dropping rows is using index slicing. This technique drops a range of rows based on their index positions.

Example

This example demonstrates how to drop the single or multiple rows from a DataFrame using the index slicing technique.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3, 4, 5],'B': [4, 5, 6, 7, 8]}) # Display original DataFrame print("Original DataFrame:") print(df) # Drop the row using the index slicing result = df.drop(df.index[2:4]) # Display the result print("\nAfter dropping the row at 2 and 3:") print(result)

Following is the output of the above code −

Original DataFrame:
A B
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
After dropping the row at 2 and 3:
A B
0 1 4
1 2 5
4 5 8
Advertisements