Reset Cumulative sum base on condition Pandas

后端 未结 2 1214
挽巷
挽巷 2021-01-18 10:56

I have a data frame like:

customer spend hurdle 
A         20    50      
A         31    50      
A         20    50      
B         50    100     
B                


        
2条回答
  •  借酒劲吻你
    2021-01-18 11:15

    One way would be the below code. But it's a really inefficient and inelegant one-liner.

    df1.groupby('customer').apply(lambda x: (x['spend'].cumsum() *(x['spend'].cumsum() > x['hurdle']).astype(int).shift(-1)).fillna(x['spend']))
    

提交回复
热议问题