Reputation: 1679
I have a df that looks like this:
event response
0 5
0 4
1 5
0 6
0 5
0 5
1 5
0 4
0 4
0 4
0 5
0 5
If df.event
indicates 1
, then the event of interest has occurred. If the event occurred, then I want to see what the response is. If the response is a value greater than 5, then I want to flag the response as True
. Due to a potential time lag, I want to look for response
values higher than 6 throughout the 3 rows preceding each event
condition.
It should look like this:
event response responseType
0 5 False
0 4 False
1 5 False
0 6 True
0 5 False
0 5 False
1 5 False
0 4 False
0 4 False
0 4 False
0 5 False
0 5 False
To be clear about the condition:
You can see that the first time event
is 1
, there is a value greater than 5
in response
within the 3 rows preceding the event condition, so True
is labeled at the point this condition is satisfied. For the second time the event condition is met, the response value does not exceed 5 within the next 3 rows of the event condition, so the responseType
stays False.
Upvotes: 0
Views: 360
Reputation: 23217
You can check current row response
> 5 and current row and last 2 rows count
equal 1 by using .shift()
as follows:
df['responseType'] = df['response'].gt(5) & (df['event'].eq(1) |
df['event'].shift(1).eq(1) |
df['event'].shift(2).eq(1))
print(df)
event response responseType
0 0 5 False
1 0 4 False
2 1 5 False
3 0 6 True
4 0 5 False
5 0 5 False
6 1 5 False
7 0 4 False
8 0 4 False
9 0 4 False
10 0 5 False
11 0 5 False
Upvotes: 1
Reputation: 150785
I think you want to checkout rolling
:
df['responseType'] = (df.event.rolling(3).max() == 1) & df.response.gt(5)
Upvotes: 0