Pandas reverse of diff()

前端 未结 3 868
小蘑菇
小蘑菇 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:48

    You can do this via numpy. Algorithm courtesy of @Divakar.

    Of course, you need to know the first item in your series for this to work.

    df = pd.DataFrame({'A': np.random.randint(0, 10, 10)})
    df['B'] = df['A'].diff()
    
    x, x_diff = df['A'].iloc[0], df['B'].iloc[1:]
    df['C'] = np.r_[x, x_diff].cumsum().astype(int)
    
    #    A    B  C
    # 0  8  NaN  8
    # 1  5 -3.0  5
    # 2  4 -1.0  4
    # 3  3 -1.0  3
    # 4  9  6.0  9
    # 5  7 -2.0  7
    # 6  4 -3.0  4
    # 7  0 -4.0  0
    # 8  8  8.0  8
    # 9  1 -7.0  1
    

提交回复
热议问题