Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I drop all rows after last occurrence of a value?

Tags:

python

pandas

I have a dataframe with a string column and I would like to drop all rows after the last occurrence of a name.

first_name
Andy
Josh
Mark
Tim
Alex
Andy
Josh
Mark
Tim
Alex
Andy
Josh
Mark

What I would like is to drop rows after Alex occurs for the last time, so drop the rows with Andy, Josh and Mark.

I figured I drop before the first occurrence with: df=df[(df.first_name== 'Alex').idxmax():], but don't know how to drop last rows.

Thanks!

like image 829
lala345 Avatar asked Mar 03 '23 20:03

lala345


1 Answers

argmax

df.iloc[:len(df) - (df.first_name.to_numpy() == 'Alex')[::-1].argmax()]

  first_name
0       Andy
1       Josh
2       Mark
3        Tim
4       Alex
5       Andy
6       Josh
7       Mark
8        Tim
9       Alex

last_valid_index

df.loc[:df.where(df == 'Alex').last_valid_index()]

Option 3

df.loc[:df.first_name.eq('Alex')[::-1].idxmax()]

Option 4

df.iloc[:np.flatnonzero(df.first_name.eq('Alex')).max() + 1]

Option 5

This is silly!

df[np.logical_or.accumulate(df.first_name.eq('Alex')[::-1])[::-1]]
like image 113
piRSquared Avatar answered Mar 12 '23 02:03

piRSquared