Data cleaning is a crucial step in any data analysis process. One common task in data cleaning involves removing unnecessary or unwanted rows from a DataFrame. Pandas, a powerful data manipulation library in Python, offers several methods to efficiently delete or filter rows. In this article, we’ll explore these methods, providing you with the tools you need to streamline your data cleaning process.
1. Deleting rows with drop() method
The drop() function in Pandas is a versatile tool for removing rows or columns from a DataFrame. To use it for deleting rows, you need to specify the index labels of the rows you want to remove. Here’s how you can do it:
1.1. Identifying and Dropping Rows by Index Labels in Pandas:
Before dropping rows in a DataFrame, it’s crucial to identify their index labels. The index label is the identifier of each row. You can view the index labels by:
print(df.index)
This command displays the index labels, which could be numbers, strings, or dates, depending on your DataFrame. After you know which rows you want to delete then you can dropping the rows.
Example-01:
Let’s walk through an example where we remove specific rows from a DataFrame:
import pandas as pd
# Creating an example DataFrame
df = pd.DataFrame({
'Column1': [1, 2, 3, 4, 5, 6, 7, 8, 9],
'Column2': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
})
print("DataFrame df: \n", df, end="\n\n")
# Specifying the row numbers (0-based indexing) to be deleted
row_numbers = [2, 5, 7]
# Retrieving the index labels for the specified rows
index_labels = df.index[row_numbers]
# Dropping the rows using the identified index labels
df.drop(index_labels, inplace=True)
# Displaying the modified DataFrame
print("Modified DataFrame: \n", df)
Output-01:

In this script:
- df is your DataFrame.
- row_numbers contains the row positions you’re interested in (2, 5, and 7). Remember that Python uses 0-based indexing, so row 2 is actually the third row, row 5 is the sixth, and so on.
- df.index[row_numbers] retrieves the index labels for these specific rows.
This approach assumes that your DataFrame has a default RangeIndex. If your DataFrame has a different index (like a DateTimeIndex or a custom index), df.index[row_numbers] will still give you the corresponding index labels for those positions. By following these steps, you can effectively remove specific rows from your DataFrame, streamlining your data for analysis. This technique is particularly useful in data cleaning and preprocessing, allowing you to tailor your dataset to your specific needs.
1.2. Using Boolean Indexing to Identify and Drop Rows in Pandas:
In addition to directly identifying index labels, Pandas allows for a more dynamic approach through boolean indexing. This method is particularly useful for filtering rows based on specific conditions and then removing them from the DataFrame.
Example-02:
Let’s see how boolean indexing can be used to identify and then drop rows:
import pandas as pd
# Creating an example DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
print("Original DataFrame (df): \n", df, end="\n\n")
# Using Boolean indexing to identify index labels of rows where 'A' > 2
index_labels = df[df['A'] > 2].index
# Dropping rows from the DataFrame based on the identified index labels
df.drop(index_labels, inplace=True)
# Displaying the DataFrame after rows have been dropped
print("Modified DataFrame after Dropping Rows: \n", df)
Output-02:

In this script:
- index_labels contains the labels of the rows that meet our condition and are to be removed.
- Setting inplace=True modifies the DataFrame df directly, applying the changes immediately.
This method is highly efficient for removing specific rows, particularly when leveraging the power of boolean indexing for row selection. It allows for a flexible and condition-based approach to refining your dataset, ensuring that your DataFrame only contains the data that meets your criteria.
1.3. Using drop() with loc and iloc in Pandas:
Pandas provides powerful indexing options like loc and iloc that, when combined with the drop() method, offer a high degree of flexibility. In this below scripts, we’ll explore how to use drop() in conjunction with loc and iloc to effectively manage your data.
Dropping Rows Using iloc:
The iloc indexer is used for position-based indexing, meaning you can select rows and columns by their integer position.
Example-03:
import pandas as pd
# Creating an example DataFrame
df = pd.DataFrame({
'A': [10, 20, 30, 40, 50, 60, 70],
'B': ['a', 'b', 'c', 'd', 'e', 'f', 'g']
})
print("Original DataFrame (df): \n", df, end="\n\n")
# Dropping the first 5 rows using iloc
df.drop(df.iloc[0:5].index, inplace=True)
print("DataFrame after Dropping First 5 Rows: \n", df)
Output-03:

In this script:
- df.iloc[0:5].index identifies the index labels of the first 5 rows (positions 0 to 4).
- drop() then removes these rows from df.
- The DataFrame is modified in place, directly reflecting the changes.
Dropping Rows Using loc: While loc is typically used for label-based indexing, it can also be combined with boolean conditions to filter rows.
Example-04:
import pandas as pd
# Creating an example DataFrame
df = pd.DataFrame({
'A': [10, 20, 30, 40, 50, 60, 70],
'B': ['a', 'b', 'c', 'd', 'e', 'f', 'g']
})
print("Original DataFrame (df): \n", df, end="\n\n")
# Using loc to identify rows where 'A' is greater than 30 and then dropping them
df.drop(df.loc[df['A'] > 30].index, inplace=True)
print("DataFrame after Dropping Rows Where 'A' > 30: \n", df)
Output-04:

In this script:
- The DataFrame df is initially displayed to show its original state.
- df.loc[df[‘A’] > 30].index identifies the index labels of rows where the values in column ‘A’ are greater than 30.
- The drop() method is then used to remove these rows from df.
- Finally, the modified DataFrame is displayed, showing the result after the specified rows have been removed.
2. Filtering Rows with isin() Method and Negation in Pandas:
In data analysis, efficiently filtering out specific rows based on certain criteria is a common task. Pandas provides a handy method, isin(), which can be combined with negation for this purpose. This approach is particularly useful when you have a list of values that you want to exclude from your analysis.
Understanding isin() with Negation for Row Filtering
The isin() method in Pandas is used to check each element in the DataFrame against a list or array of values. When you use this method in conjunction with the negation operator (~), it allows you to exclude rows that contain specified values.
Here’s a practical example to illustrate this method:
Example-05:
import pandas as pd
# Example DataFrame
df = pd.DataFrame({
'Category': ['Fruit', 'Vegetable', 'Fruit', 'Grain', 'Vegetable'],
'Item': ['Apple', 'Carrot', 'Banana', 'Wheat', 'Broccoli']
})
print("Original DataFrame: \n", df, end="\n\n")
# List of categories to exclude
categories_to_exclude = ['Fruit', 'Grain']
# Using isin() with negation to filter out specific categories
df = df[~df['Category'].isin(categories_to_exclude)]
print("DataFrame after Excluding Certain Categories: \n", df)
Output-05:

In this script:
- We start with a DataFrame df that lists various food items and their categories.
- We define categories_to_exclude containing the categories ‘Fruit’ and ‘Grain’.
- The expression df[~df[‘Category’].isin(categories_to_exclude)] is used to filter the DataFrame. The ~ operator negates the isin() condition, meaning the DataFrame will now exclude rows where the ‘Category’ column contains any of the values in categories_to_exclude.
- The final output is a DataFrame that includes only the rows not belonging to the excluded categories, in this case, ‘Vegetable’.
3. Filtering Rows Using the query() Method in Pandas
Filtering data is a fundamental aspect of data manipulation and analysis. In Pandas, the query() method offers a powerful yet intuitive way to filter DataFrame rows using a query expression. This method can be particularly useful for complex data filtering tasks, as it allows for the use of concise and readable query strings.
How to Use the query() Method
The query() method enables you to filter rows in a DataFrame based on a condition expressed as a string. This approach can simplify the syntax for complex conditions and make your code more readable.
Here’s how to use the query() method in different scenarios:
Example-06:
import pandas as pd
# Example DataFrame
df = pd.DataFrame({
'Age': [25, 30, 35, 40, 45],
'Salary': [50000, 55000, 60000, 65000, 70000]
})
print("Original DataFrame: \n", df, end="\n\n")
# Using query() to filter rows where 'Age' is not equal to 30
df_filtered = df.query("Age != 30")
print("DataFrame after Filtering 'Age' != 30: \n", df_filtered, end="\n\n")
# Using query() to filter rows where 'Salary' is greater than 60000
df_filtered = df.query("Salary > 60000")
print("DataFrame after Filtering 'Salary' > 60000: \n", df_filtered)
Output-06:

In these examples:
- The first query() filters the DataFrame to include only the rows where the ‘Age’ column is not equal to 30.
- The second query() filters the DataFrame to include only the rows where the ‘Salary’ column is greater than 60000.
The query() method evaluates the string expression and returns a new DataFrame that meets the specified condition.
4. Deleting Rows with NaN Values Using the dropna() Method in Pandas
The dropna() function in Pandas is a highly useful tool for handling missing data (NaN values) in your DataFrame. It allows you to remove rows based on the presence of NaN values in different ways, as determined by the how parameter.
4.1. how=’all’:
This parameter setting tells Pandas to drop a row only if all its values are NaN. It’s useful when you want to remove rows that are completely empty, as it ensures that rows with any valid data are retained.
Example-07:
import pandas as pd
# Creating an example DataFrame with some NaN values
df = pd.DataFrame({
'A': [1, 2, None, 4, None, None],
'B': [None, 2, 3, None, None, None],
'C': [1, 2, 3, 4, 5, None],
'D': [4, 3, 2, 1, 5, None]
})
print("Original DataFrame (df): \n", df, end="\n\n")
# Using dropna() to remove rows where all columns are NaN
# This will only drop a row if every column in that row is NaN
df.dropna(how='all', inplace=True)
print("DataFrame after Dropping Rows with All NaN Values (how='all'): \n", df, end="\n\n")
Output-07:

In this example, only the rows where every column has a NaN value will be removed from the DataFrame. This is particularly helpful in cleaning up data where entire rows are missing or irrelevant.
4.2. how=’any’ (Default Behavior):
When set to ‘any’, dropna() will drop a row if any of its values are NaN. This is the default behavior of the function.
Example-08:
import pandas as pd
# Creating an example DataFrame with some NaN values
df = pd.DataFrame({
'A': [1, 2, None, 4, None, None],
'B': [None, 2, 3, None, None, None],
'C': [1, 2, 3, 4, 5, None],
'D': [4, 3, 2, 1, 5, None]
})
print("Original DataFrame (df): \n", df, end="\n\n")
# Using dropna() to remove rows where any column is NaN
# This will drop a row if any of its columns have NaN values
df.dropna(how='any', inplace=True)
print("DataFrame after Dropping Rows with Any NaN Values (how='any'): \n", df)
Output-08:

This command will remove any row from the DataFrame that contains at least one NaN value. It’s a more aggressive approach compared to how=‘all’ and is useful when you require complete data in every row of your DataFrame.
4.3. Dropping Rows Based on NaN in Specific Columns:
To drop rows from a Pandas DataFrame where NaN values appear specifically in certain columns (say, columns A and/or B among columns A, B, C, and D), you can use a combination of subset and how parameters. Here’s how you can do it:
Example-09:
import pandas as pd
# Creating an example DataFrame with some NaN values
df = pd.DataFrame({
'A': [1, 2, None, 4, None, None],
'B': [None, 2, 3, None, None, None],
'C': [1, 2, 3, 4, 5, None],
'D': [4, 3, 2, 1, 5, None]
})
print("Original DataFrame (df): \n", df, end="\n\n")
# Using dropna() with how='all' to remove rows where all specified columns are NaN
# This will only drop a row if every specified column ('A' and 'B') in that row is NaN
df.dropna(subset=['A', 'B'], how='all', inplace=True)
print("DataFrame after Dropping Rows Where All Specified Columns Are NaN (how='all'): \n", df)
# Using dropna() with how='any' to remove rows where any specified column is NaN
# This will drop a row if any of the specified columns ('A' and 'B') have NaN values
df.dropna(subset=['A', 'B'], how='any', inplace=True)
print("DataFrame after Dropping Rows Where Any Specified Column Is NaN (how='any'): \n", df)
Output-09:

In this script:
- The DataFrame df is initially displayed to show its original state, including NaN values in various columns.
- The first dropna() method with how=’all’ is used to remove rows where all columns in the specified subset (‘A‘ and ‘B‘) are NaN. It ensures that rows are dropped only if both columns ‘A‘ and ‘B‘ are completely empty.
- The second dropna() method with how=’any’ is applied to the updated DataFrame. This command will remove rows that have NaN in any of the specified columns (‘A‘ or ‘B‘). It’s a more stringent condition, ensuring that rows with any missing values in these columns are removed.
The resulting DataFrame, displayed after each step, shows the data after these specific rows have been dropped.
5. Deleting Duplicate Rows with drop_duplicates() Method in Pandas
When working with data in Pandas, one common challenge is handling duplicate rows. These duplicates can skew your analysis and lead to misleading results. Fortunately, Pandas offers a straightforward solution with the drop_duplicates() method. This method is designed to identify and remove duplicate rows, ensuring the uniqueness of your data.
5.1. Using drop_duplicates() to Remove All Duplicate Rows
The drop_duplicates() method, when used without any parameters, removes all duplicate rows in the DataFrame based on all columns.
Example-10:
import pandas as pd
# Example DataFrame with duplicate rows
df = pd.DataFrame({
'A': [1, 2, 2, 4],
'B': ['a', 'b', 'b', 'd'],
'C': [1, 2, 2, 4]
})
print("Original DataFrame: \n", df, end="\n\n")
# Removing all duplicate rows
df.drop_duplicates(inplace=True)
print("DataFrame after Removing All Duplicates: \n", df, end="\n\n")
Output-10:

In this script, df.drop_duplicates(inplace=True) removes all rows that have identical values in every column.
5.2. Using drop_duplicates() with a Subset of Columns
Sometimes, you might want to consider duplicates based on a specific subset of columns. The drop_duplicates() method allows you to specify these columns.
Example-11:
import pandas as pd
# Example DataFrame with duplicate rows
df = pd.DataFrame({
'A': [1, 2, 2, 4],
'B': ['a', 'b', 'b', 'd'],
'C': [1, 2, 12, 4]
})
print("Original DataFrame: \n", df, end="\n\n")
# Removing duplicate rows based on a subset of columns ('A' and 'B')
df.drop_duplicates(subset=['A', 'B'], inplace=True)
print("DataFrame after Removing Duplicates Based on Columns 'A' and 'B': \n", df)
Output-11:

In this example, df.drop_duplicates(subset=[‘A’, ‘B’], inplace=True) removes rows that are duplicates in the context of columns ‘A’ and ‘B’ only.
Conclusion
In summary, this guide has provided a comprehensive overview of various methods in Pandas for deleting and filtering rows, which are essential techniques in the data cleaning process. From using the drop() method for targeted row removal based on index labels, to employing boolean indexing for condition-based filtering, and leveraging the dropna() and drop_duplicates() methods for handling missing data and duplicates, each technique offers unique advantages. These methods enable precise manipulation and refinement of DataFrames, ensuring data integrity and relevance. Understanding and applying these tools effectively is crucial in the journey of any data analyst or scientist, as clean and well-structured data is the cornerstone of insightful and accurate data analysis. By mastering these Pandas functionalities, you can significantly enhance the quality of your data preparation, leading to more robust and reliable analytical outcomes.