Pandas : Group by and count based on specific value

Question:

My deliveries dataframe looks like this :

df

     Distance    
1      10
2       0
3       0
4       3
5       2
6       0

Each time there is a 0, it means the delivery was made at the same place than the previous one. I would like to create a new column "Number of deliveries at this place" that would look like:

df2

     Distance    Nb_Deliveries  
1      10              3
2       0              3
3       0              3
4       3              1
5       2              2 
6       0              2

I don’t figure out how I could do this group by and count by taking into account the last not null (0) value.
Many thanks for your help !

Asked By: chessPL

||

Answers:

You can group by the cumsum of non-zero distances and then count

df['Nb_Deliveries'] = df.groupby(df.Distance.ne(0).cumsum()).Distance.transform('count')

Result:

   Distance  Nb_Deliveries
1        10              3
2         0              3
3         0              3
4         3              1
5         2              2
6         0              2
Answered By: Stef
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.