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

Time:06-11

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.

CodePudding user response:

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

CodePudding user response:

here is one way to do it, using index

df.drop(df[(df['Price'].isnull()) & 
           (df['Final_Price'].isnull()) & 
           (df['Available_Quantity'] > 5.0)].index)
    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
  • Related