Rolling difference in Pandas

前端 未结 4 1690
忘了有多久
忘了有多久 2020-12-10 13:39

Does anyone know an efficient function/method such as pandas.rolling_mean, that would calculate the rolling difference of an array

This is my closest so

4条回答
  •  旧时难觅i
    2020-12-10 14:03

    If you got KeyError: 0, try with iloc:

    import pandas
    
    x = pandas.DataFrame({
        'x_1': [0, 1, 2, 3, 0, 1, 2, 500, ],},
        index=[0, 1, 2, 3, 4, 5, 6, 7])
    
    x['x_1'].rolling(window=2).apply(lambda x: x.iloc[1] - x.iloc[0])
    

提交回复
热议问题