I have the following pandas.DataFrame
:
id | year | x | |
---|---|---|---|
0 | 01001 | 2015 | 0 |
1 | 01001 | 2016 | 0.5 |
2 | 01001 | 2017 | 0 |
3 | 01001 | 2018 | 0 |
4 | 01002 | 2015 | 0 |
5 | 01002 | 2016 | 0 |
6 | 01002 | 2017 | 0.0667525 |
7 | 01002 | 2018 | 0.133505 |
My goal is to create a new column and fill each window with 1
's one row after x > 0
for the first time and fill all preceding rows with 0
. That is:
id | year | x | y | |
---|---|---|---|---|
0 | 01001 | 2015 | 0 | 0 |
1 | 01001 | 2016 | 0.5 | 0 |
2 | 01001 | 2017 | 0 | 1 |
3 | 01001 | 2018 | 0 | 1 |
4 | 01002 | 2015 | 0 | 0 |
5 | 01002 | 2016 | 0 | 0 |
6 | 01002 | 2017 | 0.0667525 | 0 |
7 | 01002 | 2018 | 0.133505 | 1 |
How can this be done?
This is what I came up with: Get each group's index the first time x > 0
and fill each window with 1
from that index until the end of the partition. Then, take these indices and replace their rows with 0
.
Here's a replicable example of my data:
t = pd.DataFrame({'id':{0:'01001',1:'01001',2:'01001',3:'01001',4:'01002',5:'01002',6:'01002',7:'01002'},
'x':{0:0.0,1:0.5,2:0,3:0,4:0.0,
5:0.0,6:0.06675245612859726,7:0.13350491651818122},
'year':{0:2015,1:2016,2:2017,3:2018,4:2015,5:2016,6:2017,7:2018}})
t