Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subset df by last valid item

I can return the index of the last valid item but I'm hoping to subset a df using the same method. For instance, the code below returns the last time 2 appears in the df. But I want to return the df using this index.

import pandas as pd

df = pd.DataFrame({              
    'Number' : [2,3,2,4,2,1], 
    'Code' : ['x','a','b','c','f','y'],                          
    })

df_last = df[df['Number'] == 2].last_valid_index()

print(df_last)

4

Intended Output:

   Number Code
0       2    x
1       3    a
2       2    b
3       4    c
4       2    f
like image 960
jonboy Avatar asked Nov 25 '25 04:11

jonboy


1 Answers

You can use loc, but solution working only if at least one value 2 in column:

df = df.loc[:df[df['Number'] == 2].last_valid_index()]
print (df)
   Number Code
0       2    x
1       3    a
2       2    b
3       4    c
4       2    f

General solution should be:

df = df[(df['Number'] == 2)[::-1].cumsum().ne(0)[::-1]]
print (df)
   Number Code
0       2    x
1       3    a
2       2    b
3       4    c
4       2    f
like image 139
jezrael Avatar answered Nov 26 '25 18:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!