I have a dataframe df with 3 columns: time(timestamp), id (str) and red(boolean). I want to add another boolean column that for each row checks if this row or any of the chronologically-next two rows of this id are red. (If there are less than two rows of the same id after this row, we only consider the rows that we have.)
What is an elegant way to do this? My approach was not elegant:
I sorted by time, created an empty list called new_col and filled it in a loop over all rows of df by:
(for row_number in xrange(len(df)-2)...)
using iloc and then typed df['col']=new_col. This is slow and not very readable.