Search code examples
pythonpandasdataframegroup-by

Grouping two classes in a dataframe with start and stop time


I have a Dataframe consist of time (from a video) and label (based on occurrence of an action in the video) . I would like to get a list for each label which shows the start and finish time for every label.

Time(sec) Label
76 0
77 0
78 0
79 1
80 1
81 1
82 0
83 0
84 1

expecting output should be like this:

Label_Class_0 = [[76,78],[82,83],...]
Label_Class_1 = [[79,81],[84,..],...]

Thank you


Solution

  • Use from this code:

    df['merger']=df.label.ne(df.label.shift(1)).cumsum()
    
    df =df.groupby(by=['merger']).agg({'label':'first', 'time':['min', 'max']}).reset_index()
    df.columns=['a','b','c']
    
    df['w']=pd.Series(zip(df.b,df.c))
    
    list0=df[df.a.eq(0)].w.to_list()
    list1=df[df.a.eq(1)].w.to_list()