Dropping low/high prices instead of excluding them from df - Ebay car project

Hi, I’m trying to drop rows based on a condition rather than excluding rows from my dataframe.

I don’t understand why this piece of code doesn’t work:
autos = autos.drop(autos[(autos['price'] < 1) & (autos['price'] > 3000000)].index)

I know it doesn’t work because when I run the line below it still shows me values over 3M:
autos["price"].value_counts().sort_index(ascending = False).head()

This is not a blocker but I’d like to understand why it doesn’t work.

Thanks!

Hi @steven.nagliati

Can you please add a link to the misson, next time you pose a question (guidelines)? This makes things easier for everybody. Thank you.

As far as I can tell your code actually does work, but the conditionals for selecting the dataframe slice are wrong. You are saying: Select all observations, for which the price is below 1 and above 3,000,000. And no observation can logically fullfill this criterium and so you end up with an empty dataframe, whichs index attribute (again empty) you pass to drop(). Consequently, nothing is dropped from your original autos dataframe.

What you want is an or operator (|) between the 2 conditional statements.

Best regards
htw

2 Likes

Makes absolute sense, thank you!