How to delete a row based on column value in Pandas DataFrame

  1. .drop method to delete row on column value in Pandas dataframe
  2. boolean masking method delete row in Pandas dataframe

We will introduce the method to delete rows based on dataframe by checking conditions on column values using .drop(with and without loc), boolean masking.

.drop method to delete row on column value in Pandas dataframe

.drop method accepts a single or list of columns’ names and deletes the rows or columns. For rows we set parameter axis=0 and for column we set axis=1(by default axis is 0). We can also get the series of True and False based on condition applying on column value in Pandas dataframe.

Example Codes:

# python 3.x
import pandas as pd
fruit_list = [ ('Orange', 34, 'Yes' ) ,
             ('Mango', 24, 'No' ) ,
             ('banana', 14, 'No' ) ,
             ('Apple', 44, 'Yes' ) ,
             ('Pineapple', 64, 'No') ,
             ('Kiwi', 84, 'Yes')  ]
  
#Create a DataFrame object
df = pd.DataFrame(fruit_list, columns = 
                  ['Name' , 'Price', 'Stock']) 
# Get names of indexes for which column Stock has value No
indexNames = df[ df['Stock'] == 'No' ].index
# Delete these row indexes from dataFrame
df.drop(indexNames , inplace=True)
print(df)

Output:

     Name  Price Stock
0  Orange     34   Yes
3   Apple     44   Yes
5    Kiwi     84   Yes

We can also get a similar result by using .loc inside df.drop method.

df.drop(df.loc[df['Stock']=='Yes'].index, inplace=True)

We can also drop the rows based on multiple column values. In the above example we can delete rows which have price >= 30 and price <=70.

Example Code:

# python 3.x
import pandas as pd
# List of Tuples
fruit_list = [ ('Orange', 34, 'Yes' ) ,
             ('Mango', 24, 'No' ) ,
             ('banana', 14, 'No' ) ,
             ('Apple', 44, 'Yes' ) ,
             ('Pineapple', 64, 'No') ,
             ('Kiwi', 84, 'Yes')  ]
  
#Create a DataFrame object
df = pd.DataFrame(fruit_list, columns = 
                  ['Name' , 'Price', 'Stock'])
indexNames = df[ (df['Price'] >= 30)
                & (df['Price'] <= 70) ].index
df.drop(indexNames , inplace=True)
print(df)

Output:

     Name  Price Stock
1   Mango     24    No
2  banana     14    No
5    Kiwi     84   Yes

Rows with price > 30 and less < 70 have been deleted.

boolean masking method delete row in Pandas dataframe

boolean masking is the best and simplest way to delete row in Pandas dataframe based on column value.

Example codes:

# python 3.x
import pandas as pd
# List of Tuples
fruit_list = [ ('Orange', 34, 'Yes' ) ,
             ('Mango', 24, 'No' ) ,
             ('banana', 14, 'No' ) ,
             ('Apple', 44, 'Yes' ) ,
             ('Pineapple', 64, 'No') ,
             ('Kiwi', 84, 'Yes')  ]
  
#Create a DataFrame object
df = pd.DataFrame(fruit_list, columns = 
                  ['Name' , 'Price', 'Stock'])
print(df[df.Price > 40])
print('............................')
print(df[(df.Price > 40) & (df.Stock== 'Yes')])  

Output:

        Name  Price Stock
3      Apple     44   Yes
4  Pineapple     64    No
5       Kiwi     84   Yes
............................
    Name  Price Stock
3  Apple     44   Yes
5   Kiwi     84   Yes

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Convert a float to an integer in Pandas DataFrame
  • How to Sort Pandas DataFrame by One Column's Values
  • How to get the aggregate of Pandas group-by and Sum
  • How to convert Python dictionary to Pandas DataFrame
  • How to add header row to a pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to convert index of a Pandas Dataframe into a column
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to add one row to Pandas DataFrame
  • How to get a value from a cell of a Pandas DataFrame
  • comments powered by Disqus