
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
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.
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 |
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.
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 |
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.
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 |
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.
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 |
A | B | |
---|---|---|
0 | 1 | 4 |
1 | 2 | 5 |
4 | 5 | 8 |