Please find below my input/desired output :
INPUT
Id Status Date
0 Id001 off 01/01/2022
1 Id001 online 01/06/2022
2 Id002 off 05/12/2021
3 Id002 off 30/08/2021
4 Id003 running 02/03/2021
5 Id004 online 21/10/2021
6 Id004 running 01/04/2022
7 Id005 online 01/02/2022
8 Id005 online 02/07/2022
OUTPUT (desired)
Id Status Date
0 Id001 online 01/06/2022
1 Id002 off 05/12/2021
2 Id003 running 02/03/2021
3 Id004 online 21/10/2021
4 Id005 online 01/02/2022
The goal is firstly to have one line per Id in the output.
The output will be made based on a this simple statement :
if a group of Id has «online» as a Status then:
if «online» occurences > 1 then:
Choose the oldest Id that has «online»
else:
choose the Id that has «online»
else:
Choose the most recent Id
This is what I've tried so far :
import pandas as pd
import numpy as np
df = pd.DataFrame({'Id': ['Id001','Id001','Id002','Id002','Id003','Id004','Id004','Id005','Id005'],
'Status': ['off','online','off','off','running','online','running','online','online'],
'Date': ['01/01/2022','01/06/2022','05/12/2021','30/08/2021','02/03/2021','21/10/2021','01/04/2022','01/02/2022','02/07/2022']})
df.groupby(['Id', 'Status'], as_index=False).agg({'Date' : [np.min, np.max]}).reset_index()
Do you have any suggestion/propositions, please ?
Any help we be so much appreciated !