You can do this by reshaping your datetime value to hourly before grouping. I opted to create a new column with the "fixed" time and group by it:
Create demo df:
import pandas as pd
from io import StringIO
csv_string = StringIO("""LOB, timestamp, Transaction, Hits
PRO, 2020-09-03 17:51:16, LOGIN, 1
PRO, 2020-09-03 17:51:15, ELG, 1
PRO, 2020-09-03 17:51:12, LOGIN, 4
PRO, 2020-09-03 17:51:13, ELG, 11
PRO, 2020-09-03 17:51:14, LOGIN, 3
PRO, 2020-09-03 17:51:11, ELG, 2
PRO, 2020-09-03 18:51:12, LOGIN, 24
PRO, 2020-09-03 18:51:13, ELG, 21
PRO, 2020-09-03 18:51:14, LOGIN, 23
PRO, 2020-09-03 18:51:11, ELG, 22""" )
df = pd.read_csv(csv_string, sep=",", skipinitialspace=True)
and work with it:
# convert timestamp column to datetime
df["timestamp"] = pd.to_datetime(df["timestamp"])
# create a fixed time column with hours
# cudos: https://stackoverflow.com/a/43400370/7505395
df["by_hour"] = pd.to_datetime(df["timestamp"].dt.date) + \
pd.to_timedelta(df["timestamp"].dt.hour, unit="H")
print(df)
# group by, use as index
grouped = df.groupby(by=["by_hour", "Transaction"], as_index=True)
# sum and print
print(grouped.sum())
Output:
LOB timestamp Transaction Hits by_hour
0 PRO 2020-09-03 17:51:16 LOGIN 1 2020-09-03 17:00:00
1 PRO 2020-09-03 17:51:15 ELG 1 2020-09-03 17:00:00
2 PRO 2020-09-03 17:51:12 LOGIN 4 2020-09-03 17:00:00
3 PRO 2020-09-03 17:51:13 ELG 11 2020-09-03 17:00:00
4 PRO 2020-09-03 17:51:14 LOGIN 3 2020-09-03 17:00:00
5 PRO 2020-09-03 17:51:11 ELG 2 2020-09-03 17:00:00
6 PRO 2020-09-03 18:51:12 LOGIN 24 2020-09-03 18:00:00
7 PRO 2020-09-03 18:51:13 ELG 21 2020-09-03 18:00:00
8 PRO 2020-09-03 18:51:14 LOGIN 23 2020-09-03 18:00:00
9 PRO 2020-09-03 18:51:11 ELG 22 2020-09-03 18:00:00
Hits
by_hour Transaction
2020-09-03 17:00:00 ELG 14
LOGIN 8
2020-09-03 18:00:00 ELG 43
LOGIN 47