I have a dataset with the Moon Phases. I want to create a new column that counts the days from Fase One to Fase Four. Starting again from 1 once a Fase One starts. So at the end, my counter would have from 1 to 27 or 28 days.
I checked this link but I haven't manages to start the counting from 1 again Counter Column in Pandas DataFrame where it changes based on other column. I tried with a for but is not giving me the result I expected
I tried with a for and with out the for
for i in Moon.phaseIdM:
Moon['phaseMDay'] = (Moon.phaseIdM.eq(1)
& Moon.phaseIdM.shift().eq(4)).cumsum() + 1
I expect:
phaseM | phaseMday
1 | 1
1 | 2
2 | 3
2 | 4
3 | 5
4 | 6
4 | 7
1 | 1
2 | 2 ...
What I get:
phaseM | phaseMday
1 | 1
1 | 1
2 | 1
2 | 1
3 | 1
4 | 1
4 | 1
1 | 2
2 | 2 ...
Thanks in advance for your help