[FIXED] Every row after a certain value

Issue

Here is part of my data: src="https://i.stack.imgur.com/KiF4a.png" alt="data" />

I want to create a dataframe with every row after the row with L = ball_snap in it. So not only that specific row but every row after it as well.

How would I do that in pandas?


Solution

You can use idxmax to return the row with L = ball_snap and use the slice notation to extract all rows after:

df1 = df.loc[df['L'].eq('ball_snap').idxmax():]

Input:

>>> df
   A  B  C          L
0  8  6  9           
1  3  1  6           
2  4  9  9           
3  1  5  1  ball_snap
4  3  7  0           
5  9  2  0           
6  1  8  4           
7  7  9  8           
8  5  9  1           
9  4  3  4           

Output:

>>> df1
   A  B  C          L
3  1  5  1  ball_snap
4  3  7  0           
5  9  2  0           
6  1  8  4           
7  7  9  8           
8  5  9  1           
9  4  3  4           

Update: if you don't want the row with 'ball_snap' use shift before idxmax:

df2 = df.loc[df['L'].eq('ball_snap').shift(fill_value=False).idxmax():]

Output:

>>> df2
   A  B  C L
4  3  7  0  
5  9  2  0  
6  1  8  4  
7  7  9  8  
8  5  9  1  
9  4  3  4  


Answered By - Corralien
Answer Checked By - Clifford M. (sql.easybugfix Volunteer)

Related Posts

Post a Comment