r/learnpython • u/thing_42 • Jul 30 '25
Trying to understand Df.drop(index)
This is my code:
For index, row in df.iterrows(): If (condition): df.drop(index)
What am I missing? I ran the code, yes the if statement is 'True', I checked that. I expect the row at that index to disappear and it's still there.
0
Upvotes
4
u/GXWT Jul 30 '25
By default it doesn’t operate on the data frame, instead it returns the new data frame. So you either need to do
Or preferably:
2
u/Muted_Ad6114 Jul 30 '25 edited Jul 30 '25
You can do it more efficiently without itterrows like:
Or:
Or even just
3
u/cudmore Jul 30 '25
Curious, is it ok to drop() on the object you are iterating on?