Q: Python - filtering lines from data frame I have a simple data frame: 
ID  Stime       Etime
1   13:00:00    13:15:00
1   14:00:00    14:15:00
2   15:00:00    15:42:00
3   13:00:00    13:25:00
4   15:00:00    15:15:00
4   15:05:00    15:15:00

What I would like to do is to unit the 2 last lines, because they belong to the same ID (ID=4) and the time of the last line is contained in the time of the penultimate line. 
What I want the output to be is:
ID  Stime       Etime
1   13:00:00    13:15:00
1   14:00:00    14:15:00
2   15:00:00    15:42:00
3   13:00:00    13:25:00
4   15:00:00    15:15:00


A: Solution
def setup(df):
    td = df.Stime - df.Etime.shift()
    td = td.apply(lambda x: x.total_seconds() > 1)
    td.iloc[0] = True
    return td.cumsum()

def collapse(df):
    df_ = df.iloc[0, :]
    df_.loc['Stime'] = df.Stime.min()
    df_.loc['Etime'] = df.Etime.max()
    return df_

df['group id'] = df.groupby('ID').apply(setup).values

gbcols = ['ID', 'group id']
fcols = ['ID', 'Stime', 'Etime']

print df.groupby(gbcols)[fcols].apply(collapse).reset_index(drop=True)

   ID               Stime               Etime
0   1 2016-05-30 13:00:00 2016-05-30 13:15:00
1   1 2016-05-30 14:00:00 2016-05-30 14:15:00
2   2 2016-05-30 15:00:00 2016-05-30 15:42:00
3   3 2016-05-30 13:00:00 2016-05-30 13:25:00
4   4 2016-05-30 15:00:00 2016-05-30 15:15:00

