Cumsum reset at NaN

后端 未结 4 1393
醉话见心
醉话见心 2020-11-27 05:21

If I have a pandas.core.series.Series named ts of either 1\'s or NaN\'s like this:

3382   NaN
3381   NaN
...
3369   NaN
3368   NaN
         


        
4条回答
  •  眼角桃花
    2020-11-27 06:26

    If you can accept a similar boolean Series b, try

    (b.cumsum() - b.cumsum().where(~b).fillna(method='pad').fillna(0)).astype(int)
    

    Starting from your Series ts, either b = (ts == 1) or b = ~ts.isnull().

提交回复
热议问题