I have a dataset like the following:
start_date finish_date
0 2020-06-01 2020-06-02
1 2020-06-02 2020-06-04
2 2020-06-03 NaT
3 2020-06-04 2020-06-07
4 2020-06-05 2020-06-07
5 2020-06-06 NaT
6 2020-06-07 NaT
which can be re-created using the following code:
import pandas as pd
df = pd.DataFrame({
'start_date': ['2020-06-01', '2020-06-02', '2020-06-03', '2020-06-04', '2020-06-05', '2020-06-06', '2020-06-07'],
'finish_date': ['2020-06-02', '2020-06-04', pd.NA, '2020-06-07', '2020-06-07', pd.NA, pd.NA],
})
df['start_date'] = df['start_date'].apply(pd.to_datetime)
df['finish_date'] = df['finish_date'].apply(pd.to_datetime)
The question is: how to get the count of rows which have no finish_date or not yet finished by the reporting_date, the following is the expected result:
reporting_date not_finished
0 2020-06-01 1
1 2020-06-02 1
2 2020-06-03 2
3 2020-06-04 2
4 2020-06-05 3
5 2020-06-06 4
6 2020-06-07 3
To explain the expected result above:
(when I say row, I refer to the row in the dataset not the result)
- By
reporting_date2020-06-01, the row0has started but not finished, that counts1. - By
reporting_date2020-06-02, the row0has already finished and row1has started but not finished yet, that counts1. - By
reporting_date2020-06-03, the rows1and2have started but not finished yet, that counts2. - By
reporting_date2020-06-04, the row2has already finished and rows2and3have started but not finished yet, that counts2. - and so on..