Pandas provide various functions to clean data before analyzing it. Dropping rows remains one such operation which is very important during cleaning stage. There can various rows, or uncleaned rows which are note useful for analysis. Also, there can be cases where user wants to remove most part of the data to make analysis faster. To achieve this dropping rows by condition is the best approach. This article covers Steps to drop rows by condition in Pandas, Python.
John has employee data available with him. He is looking forward to drop few rows based on certain conditions. He wants to drop rows for employee name Levon. Also he wants to create another dataframe where he is looking to drop rows for Levon as well as all male employees.
Below are the key steps to drop necessary rows:
- Step 1: Firstly import necessary modules.
import pandas as pd
- Step 2: Then use index to define index of the rows which are needed to be dropped. As Levon is first name of the employee. Index will contain first name parameter comparing it with “Levon”. Please see below the code.
Index_def = Employee_data[Employee_data['first_name']=='Levon'].index
- Step 3: Use Dataframe drop function to drop the rows. Provide Index_def in paranthesis.
Employee_data2=Employee_data.drop(Index_def)
- Step 4: Check Data quality by entering the dataframe name and pressing run button.
Employee_data2
Example 2: Dropping rows based on multiple condition
- Step 1: The 2 conditions are, Employe name is Levon or Employee Gender is Male. We will define index to include both the condition. First part defines employee name condition (Employee_data[‘first_name’]==’Levon’). Second part defines employee gender condition (Employee_data[‘gender’]==’Male’). “|” this denotes OR operation.
Index_def = Employee_data[(Employee_data['first_name']=='Levon') | (Employee_data['gender']=='Male')].index
- Step 2: Use Dataframe drop function to drop the rows. Provide Index_def in paranthesis as was done in previous example.
Employee_data2=Employee_data.drop(Index_def)
- Step 3: Check Data quality by entering the dataframe name and pressing run button.
Employee_data2
Thus, John is able to create Dataframe as per his requirement in Python.
To get top certifications in Python and build your resume visit here. Also, you can read books listed here to build strong knowledge around Python.
Visit us below for video tutorial:
Looking to practice more with this example? Drop us a note, we will email you the Code file:
📬 Stay Ahead in Data Science & AI – Subscribe to Newsletter!
- 🎯 Interview Series: Curated questions and answers for freshers and experienced candidates.
- 📊 Data Science for All: Simplified articles on key concepts, accessible to all levels.
- 🤖 Generative AI for All: Easy explanations on Generative AI trends transforming industries.
💡 Why Subscribe? Gain expert insights, stay ahead of trends, and prepare with confidence for your next interview.
👉 Subscribe here: