Running sum in pandas (without loop)

后端 未结 1 610
情歌与酒
情歌与酒 2020-11-30 11:35

I\'d like to build a running sum over a pandas dataframe. I have something like:

10/10/2012:  50,  0
10/11/2012: -10, 90
10/12/2012: 100, -5
<
相关标签:
1条回答
  • 2020-11-30 12:03

    As @JonClements mentions, you can do this using the cumsum DataFrame method:

    from pandas import DataFrame
    df = DataFrame({0: {'10/10/2012': 50, '10/11/2012': -10, '10/12/2012': 100}, 1: {'10/10/2012': 0, '10/11/2012': 90, '10/12/2012': -5}})
    
    In [3]: df
    Out[3]: 
                  0   1
    10/10/2012   50   0
    10/11/2012  -10  90
    10/12/2012  100  -5
    
    In [4]: df.cumsum()
    Out[4]: 
                  0   1
    10/10/2012   50   0
    10/11/2012   40  90
    10/12/2012  140  85
    
    0 讨论(0)
提交回复
热议问题