Home > Blockchain >  python, pandas, drop rows by condition
python, pandas, drop rows by condition

Time:10-01

hello I need help to drop some rows by the condition: if the estimated price minus price is more than 1500 (positive) drop the row

    price      estimated price 
 0  13295                13795
 1  19990                22275
 2   7295                 6498

for example only the index 1 would be drop thank you!

CodePudding user response:

Change logic for get all rows if less or equal like 1500 after subtracting in boolean indexing with Series.le:

df1 = df[df['estimated price'].sub(df['price']).le(1500)]

working like invert mask for greater like 1500:

df1 = df[~df['estimated price'].sub(df['price']).gt(1500)]

CodePudding user response:

You can use pd.drop():

>>> df.drop(df[(df['estimated price']-df['price'] >= 1500)].index)

   price  estimated price
0  13295            13795
2   7295             6498

index 1 is dropped.

  • Related