I create a groupby data frames like this:
df['Datetime'] = pd.to_datetime(df.Date) + pd.to_timedelta(df.Hour, unit='h')
grouped = df.set_index('Datetime').groupby(pd.Grouper(freq=horizon))
However, I would like to be able to print the first row of the current group in the loop and the last row of the previous group in the list grouped[group-1]. The idea is to get something like this:
for name, group in grouped:
print("first row of the current group")
print("last row of the previous group in the loop")
And if its the first group in grouped, then only print the first row of the current group.
Does anyone have idea on how to do this? I have formulated as general so I could apply it for something a little bit different but that has the same idea behind.
Thanks in advance!