2

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 !

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.