Home > Mobile >  Groupby check last rows values, and return validation, pandas
Groupby check last rows values, and return validation, pandas

Time:08-18

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]})

CodePudding user response:

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()

CodePudding user response:

df.groupby('Id').agg({
    'LastState': lambda x: 'POTATO IS A FRUIT' in list(x)[-2:]
})

for check to string contains:

def finder(string, lst):
    return any([string in l for l in lst])
df.groupby('Id').agg({'LastState': lambda x: finder('POTATO IS A FRUIT', list(x)[-2:])})
  • Related