I am trying to iterate over rows in a pandas Dataframe with a conditional count in a new column called Stage. For each name the stage should start at 1, and if the name is the same between rows then after a "Healthy" status a new stage should start. A "Healthy" event will be in the same stage as the preceding "Sick" events, if they exist. I've done the code in excel before but not sure how to do it in python.
What I have now is:
| Date | Name | Status |
|---|---|---|
| 2020-01-02 | Mary | Healthy |
| 2020-01-05 | Mary | Sick |
| 2020-01-15 | Mary | Sick |
| 2020-01-20 | Mary | Healthy |
| 2020-02-03 | Mary | Healthy |
| 2020-02-06 | Mary | Sick |
| 2020-02-10 | Mary | Sick |
| 2020-02-15 | Mary | Healthy |
| 2020-01-02 | Bob | Healthy |
| 2020-01-05 | Bob | Healthy |
| 2020-01-15 | Bob | Healthy |
| 2020-01-20 | Bob | Sick |
| 2020-02-03 | Bob | Sick |
| 2020-02-06 | Bob | Sick |
| 2020-02-10 | Bob | Sick |
| 2020-02-15 | Bob | Healthy |
What I would like to have:
| Date | Name | Status | Stage |
|---|---|---|---|
| 2020-01-02 | Mary | Healthy | 1 |
| 2020-01-05 | Mary | Sick | 2 |
| 2020-01-15 | Mary | Sick | 2 |
| 2020-01-20 | Mary | Healthy | 2 |
| 2020-02-03 | Mary | Healthy | 3 |
| 2020-02-06 | Mary | Sick | 4 |
| 2020-02-10 | Mary | Sick | 4 |
| 2020-02-15 | Mary | Healthy | 4 |
| 2020-01-02 | Bob | Healthy | 1 |
| 2020-01-05 | Bob | Healthy | 2 |
| 2020-01-15 | Bob | Healthy | 3 |
| 2020-01-20 | Bob | Sick | 4 |
| 2020-02-03 | Bob | Sick | 4 |
| 2020-02-06 | Bob | Sick | 4 |
| 2020-02-10 | Bob | Sick | 4 |
| 2020-02-15 | Bob | Healthy | 4 |