How to Drop rows in DataFrame by conditions on column values? - GeeksforGeeks

02 Jul, 2020
In this article, we are going to see several examples of how to drop rows from the dataframe based on certain conditions applied on a column.
Pandas provide data analysts a way to delete and filter data frame using dataframe.drop() method. We can use this method to drop such rows that do not satisfy the given conditions.
Let’s create a Pandas dataframe.
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya',              'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU',                     'Geu', 'Geu'],}  # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age',                                      'University'],                  index = ['a', 'b', 'c', 'd', 'e',                           'f'])  df
Output:
Example 1 : Delete rows based on condition on a column.
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya',              'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU',                     'Geu', 'Geu'],}  # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age',                                      'University'],                  index = ['a', 'b', 'c', 'd', 'e', 'f'])  # get names of indexes for which# column Age has value 21index_names = df[ df['Age'] == 21 ].index  # drop these row indexes# from dataFramedf.drop(index_names, inplace = True)  df
Output :
Example 2 : Delete rows based on multiple conditions on a column.
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya',               'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU',                    'Geu', 'Geu'],}  # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age',                                      'University'],                  index = ['a', 'b', 'c', 'd', 'e', 'f'])  # get names of indexes for which column Age has value >= 21# and <= 23index_names = df[ (df['Age'] >= 21) & (df['Age'] <= 23)].index  # drop these given row# indexes from dataFramedf.drop(index_names, inplace = True)  df
Output :
Example 3 : Delete rows based on multiple conditions on different columns.
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya',              'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU',                     'Geu', 'Geu'],}  # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age',                                      'University'],                  index = ['a', 'b', 'c', 'd', 'e', 'f'])  # get names of indexes for which# column Age has value >= 21# and column University is BHUindex_names = df[ (df['Age'] >= 21) & (df['University'] == 'BHU')].index  # drop these given row# indexes from dataFramedf.drop(index_names, inplace = True)  df
Output :
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Python String | replace()
Reading and Writing to text files in Python
sum() function in Python