Groupby check last rows values, and return validation, pandas

I have the following df:

df = pd.DataFrame({'Id':[1,1,1,2,2,2,3,3,3],
                   'LastState':['JAY','JAY','JAY',np.nan,'penu hehe','penu hehe','JAY','penu hehe','POTATO IS A FRUIT']})

Is there a way i could check if the last two rows in the grouped series id, contains a specific string in this case ‘POTATO IS A FRUIT’, and then returning True or False on the group?

Wanted result

result = pd.DataFrame({'Id':[1,2,3],'LastState':[False,False,True]})

>Solution :

To check if any of the last 2 rows of each group contain 'POTATO IS A FRUIT':

df.groupby('Id').agg(lambda g: 
                        g.iloc[-2:].str.contains('POTATO IS A FRUIT').any()
                    ).reset_index()

Leave a Reply