Let's say I have this DataFrame
time status
0 10:00 On
1 10:01 Off
2 10:02 Off
3 10:03 Off
4 10:04 On
5 10:05 Off
6 10:06 Off
7 10:07 Off
8 10:08 Off
9 10:09 On
10 10:10 Off
I want to add a column that shows the previous time the status was "on", as follows
time status last_on
0 10:00 On NaN
1 10:01 Off 10:00
2 10:02 Off 10:00
3 10:03 Off 10:00
4 10:04 On 10:00
5 10:05 Off 10:04
6 10:06 Off 10:04
7 10:07 Off 10:04
8 10:08 Off 10:04
9 10:09 On 10:04
10 10:10 Off 10:09
Any thoughts?
CodePudding user response:
you could try something like this:
df['last_on']= df.time.where(df.status=='On').shift().ffill()