Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, pandas, drop rows by condition

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!

like image 875
Regnas01 Avatar asked Oct 24 '25 19:10

Regnas01


1 Answers

You can use pd.drop() in which you can drop specific rows by index. :

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

   price  estimated price
0  13295            13795
2   7295             6498

index 1 is dropped.

Note that this method assumes that your index is unique. If otherwise, boolean indexing is the better solution.

like image 141
sophocles Avatar answered Oct 26 '25 10:10

sophocles