Pandas reverse of diff()

前端 未结 3 843
小蘑菇
小蘑菇 2021-01-11 18:33

I have calculated the differences between consecutive values in a series, but I cannot reverse / undifference them using diffinv():

ds_sqrt =         


        
3条回答
  •  心在旅途
    2021-01-11 18:44

    df.cumsum()

    Example:
    data = {'a':[1,6,3,9,5], 'b':[13,1,2,5,23]}
    df = pd.DataFrame(data)
    
    df = 
        a   b
    0   1   13
    1   6   1
    2   3   2
    3   9   5
    4   5   23
    
    df.diff()
    
    a   b
    0   NaN NaN
    1   5.0 -12.0
    2   -3.0    1.0
    3   6.0 3.0
    4   -4.0    18.0
    
    df.cumsum()
    
    a   b
    0   1   13
    1   7   14
    2   10  16
    3   19  21
    4   24  44
    

提交回复
热议问题