I have a pandas dataframe and would like to create a new column with values based on conditions from the first and last row of a groupby. The conditions required are
mgr to mgr = hired as mgr
emp to mgr = promoted to mgr
emp to emp = hired as emp
mgr to emp = status change
date email level
01/01/2000 john@abc.com mgr
05/06/2000 john@abc.com mgr
10/01/2001 john@abc.com mgr
14/02/2000 kimdo@abc.com emp
19/10/2001 kimdo@abc.com mgr
12/05/2000 waint@abc.com emp
08/08/2000 waint@abc.com emp
14/04/2001 waint@abc.com emp
22/05/2000 neds@abc.com mgr
08/11/2000 neds@abc.com mgr
12/06/2001 neds@abc.com emp
Would like to achieve the results below
date email level status
01/01/2000 john@abc.com mgr hired as mgr
10/01/2001 john@abc.com mgr hired as mgr
14/02/2000 kimdo@abc.com emp promoted to mgr
19/10/2001 kimdo@abc.com mgr promoted to mgr
12/05/2000 waint@abc.com emp hired as emp
14/04/2001 waint@abc.com emp hired as emp
22/05/2000 neds@abc.com mgr status change
12/06/2001 neds@abc.com emp status change
So far, am able to select the first and last rows of the dataframe based on a groupyby, but am not entirely sure how to apply the conditions to get the new 'status' column. Appreciate any form of help, thank you.
df2 = df.groupby('email', as_index=False).nth([0,-1])