4

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!

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes it only works with unique index. I assumed that though, based on the sample data we have above.
thanks for the tip, please continue pointing these things out. we are all here to learn :)
ya, only thinking sometimes about another solutions ;)
4

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)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.