I'm trying to group a Pandas dataframe by date based on one datetime column and, based on that, count the number of specific occurrences in another column based on a specific value. Let's say I have this dataframe:
df = pd.DataFrame({
"customer": [
"A", "A", "A", "A", "A", "B", "C", "C"
],
"datetime": pd.to_datetime([
"2020-01-01 00:00:00", "2020-01-02 00:00:00", "2020-01-02 01:00:00", "2020-01-03 00:00:00", "2020-01-04 00:00:00", "2020-01-03 00:00:00", "2020-01-03 00:00:00", "2020-01-04 00:00:00"
]),
"enabled": [
True, True, False, True, True, True, False, True
]
})
The dataframe looks like this:
customer datetime enabled
A 2020-01-01 00:00:00 True
A 2020-01-02 00:00:00 True
A 2020-01-02 01:00:00 False
A 2020-01-03 00:00:00 True
A 2020-01-04 00:00:00 True
B 2020-01-03 00:00:00 True
C 2020-01-03 00:00:00 False
C 2020-01-04 00:00:00 True
I would like to count, at the end of each day, the number of enabled customers. If a customer is enabled, it remains enabled for the following days, unless there's an enabled==False row on a later day. The expected output would be:
day count_enabled_customers
2020-01-01 1 # A
2020-01-02 0 # A has been disabled
2020-01-03 2 # A, B
2020-01-04 3 # A, B, C
Does someone have an idea of how to proceed with this? Thanks a lot in advance!