Dataframe removes duplicate when certain values are reached
You can use cumsum()
on the threshold differences to identify the group and groupby on that:
groups = (df.groupby(['Action', 'Name'])['Time']
.transform(lambda x: x.diff().gt('5min').cumsum())
)
df.groupby([groups,'Action','Name'], as_index=False).head(1)
Output:
Time Action Name
0 2019-01-10 09:56:52 Opened Max
2 2019-02-10 12:56:12 Closed Susan
3 2019-02-10 13:02:58 Opened Michael
IIUC you can create a group
number by getting the time difference, and then groupby
and first
:
print (df.assign(group=pd.to_datetime(df["Time"]).diff().dt.seconds.gt(300).cumsum())
.groupby(["group", "Action", "Name"]).first())
Time Action Name
group
0 01.10.2019, 9:56:52 Opened Max
1 02.10.2019 12:56:12 Closed Susan
2 02.10.2019 13:02:58 Opened Michael
3 02.10.2019 13:11:58 Opened Michael