Loop the removal of pandas dataframe rows
Solution 1:
You can use a boolean mask to slice your data. If your df['time']
is datetime.time objects, then you can slice df
simply as:
out = df[df['time'] > datetime.time(4,30)]
Output:
ticker date time vol vwap open high low close
5 AACG 2022-01-06 04:31:00 700 2.1098 2.1 2.11 2.1 2.11
Solution 2:
Don't loop but slice. You can use a mask for that (here generated with a boolean array and cummax
):
df[df['time'].eq('04:30:00').cummax()]
output:
ticker date time vol vwap open high low close
4 AACG 2022-01-06 04:30:00 237 2.0584 2.06 2.06 2.06 2.06
5 AACG 2022-01-06 04:31:00 700 2.1098 2.10 2.11 2.10 2.11
If you also want to exclude the matching row:
df[df['time'].eq('04:30:00').shift(fill_value=False).cummax()]