I am trying to create a new column where in, True if last n rows are True in other column. It works fine but the problem is it is taking lot of time. This is my code:
dfx = pd.DataFrame({'A':[False,False,False,False,True,True,True,True,False,True]})
n=2 ## n to cover 10 min range samples
cl_id = dfx.columns.tolist().index('A') ### cl_id for index number of the column for using in .iloc
l1=[False]*n+[all(dfx.iloc[x+1-n:x+1,cl_id].tolist()) for x in np.arange(n,len(dfx))]
dfx['B'] = l1
print(dfx)
#old_col # New_col
A B
0 False False
1 False False
2 False False
3 False False
4 True False
5 True True ## Here A col last two rows True, hence True
6 True True ## Here A col last two rows True, hence True
7 True True ## Here A col last two rows True, hence True
8 False False
9 True False