Pandas: How to delete the rows that meet conditions by filter?

Issue

import pandas as pd

data={"product_name":["Keyboard","Mouse", "Monitor", "CPU","CPU", "Speakers",pd.NaT],
      "Price":[500,None, 5000.235, None, 10000.550, 250.50,None],
      "Final_Price":[5,None, 10, None, 20, 8,None],
      "Available_Quantity":[5,9,6,None,6, 5,8],
      "Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','09/18/2021','01/05/2021',pd.NaT]
   }

df = pd.DataFrame(data)
df

The filter to find the rows that meet the condition

myfilter= (df.query("Price=='Nan' and Final_Price=='Nan'and Available_Quantity >=5 "))
myfilter

It works because I found the rows that I want. Now I want to delete that rows of the DataFrame but not using df.loc or df.iloc because in a bigger DataFrame that would not be useful.

Solution

I am not sure what you want though using query we could do:

df.query('Price.isna() & Final_Price.isna() & Available_Quantity > 5')

  product_name  Price  Final_Price  Available_Quantity Available_Since_Date
1        Mouse    NaN          NaN                 9.0            4/23/2021
6          NaT    NaN          NaN                 8.0                  NaT

df.query('~(Price.isna() & Final_Price.isna() & Available_Quantity > 5)') 

  product_name      Price  Final_Price  Available_Quantity Available_Since_Date
0     Keyboard    500.000          5.0                 5.0            11/5/2021
2      Monitor   5000.235         10.0                 6.0           08/21/2021
3          CPU        NaN          NaN                 NaN           09/18/2021
4          CPU  10000.550         20.0                 6.0           09/18/2021
5     Speakers    250.500          8.0                 5.0           01/05/2021

Answered By – onyambu

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published