Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropna only on the last n rows Python

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
like image 845
Vedran Avatar asked Dec 19 '25 04:12

Vedran


1 Answers

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
like image 198
BENY Avatar answered Dec 21 '25 19:12

BENY