I'm trying to clean a dataset. In the last 3 rows, I know if the column "B" is empty drop the whole row. I haven't managed to figure out how to use dropna only on certain rows.
A B
1 1 3
2 5
3 6 5
4 2
5 3 6
Needs to become
A B
1 1 3
2 5
3 6 5
5 3 6
You slice the last three row then apply your condition pass that to drop
n=3
df=df.drop(df.tail(n).B.eq('').loc[lambda x : x].index)
A B
1 1 3
2 5
3 6 5
5 3 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With