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 library import pandas as pd # dictionary with list object in values details = { '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 library import pandas as pd # dictionary with list object in values details = { '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 index_names = df[ df['Age'] == 21 ].index # drop these row indexes # from dataFrame df.drop(index_names, inplace = True) df |
Output :

Example 2 : Delete rows based on multiple conditions on a column.
# import pandas library import pandas as pd # dictionary with list object in values details = { '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 <= 23 index_names = df[ (df['Age'] >= 21) & (df['Age'] <= 23)].index # drop these given row # indexes from dataFrame df.drop(index_names, inplace = True) df |
Output :

Example 3 : Delete rows based on multiple conditions on different columns.
# import pandas library import pandas as pd # dictionary with list object in values details = { '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 BHU index_names = df[ (df['Age'] >= 21) & (df['University'] == 'BHU')].index # drop these given row # indexes from dataFrame df.drop(index_names, inplace = True) df |
Output :

Recommended Posts:
- Drop rows from the dataframe based on certain condition applied on a column
- How to Drop Rows with NaN Values in Pandas DataFrame?
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- How to drop rows in Pandas DataFrame by index labels?
- Drop a list of rows from a Pandas DataFrame
- Python - Extract ith column values from jth column values
- Selecting rows in pandas DataFrame based on conditions
- Find maximum values & position in columns and rows of a Dataframe in Pandas
- Sort rows or columns in Pandas Dataframe based on values
- Get minimum values in rows or columns with their index position in Pandas-Dataframe
- Create a DataFrame from a Numpy array and specify the index column and column headers
- Get column index from column name of a given Pandas DataFrame
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Get unique values from a column in Pandas DataFrame
- Get n-smallest values from a particular column in Pandas DataFrame
- Get n-largest values from a particular column in Pandas DataFrame
- Getting Unique values from a column in Pandas dataframe
- How to Concatenate Column Values in Pandas DataFrame?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

