The whole question is to calculate the average value of the next n lines in the n column relative to the current element, i.e. 1 => mean([2, 3, 4]); 2 => mean([3, 4, 5]) 1 => mean([2, 3, 4]); 2 => mean([3, 4, 5]) .

Simple test column with one column:

 import pandas as pd df = pd.DataFrame({1: range(20)}) 

And there is a solution to my question, but how to solve it with the help of pandas? This is an easy task, and it’s hard to understand how this whole mechanism is iterated into pandas. Therefore, I made the generator such:

 def idx(x, step): result = [] for i in range(len(x) - step): result.append(x.shift(-1)[i:i + step].mean()) return pd.DataFrame(result) df[2] = idx(df[1], 3) print(df) 

    1 answer 1

    If I correctly understood the task: calculate the average in a floating window (the window starts with the following relative to the current element) of N elements:

    Source DataFrame:

     In [56]: df = pd.DataFrame({'col1': range(20)}) In [57]: df Out[57]: col1 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 

    Solution - use Series.rolling () + window.Rolling.mean () :

     In [63]: N = 3 In [64]: df['col2'] = df.col1.shift().rolling(N, min_periods=1).mean() In [65]: df Out[65]: col1 col2 0 0 NaN 1 1 0.0 2 2 0.5 3 3 1.0 4 4 2.0 5 5 3.0 6 6 4.0 7 7 5.0 8 8 6.0 9 9 7.0 10 10 8.0 11 11 9.0 12 12 10.0 13 13 11.0 14 14 12.0 15 15 13.0 16 16 14.0 17 17 15.0 18 18 16.0 19 19 17.0 

    if the middle of the window should fall on the current element:

     In [71]: df['col2'] = df.col1.rolling(N, center=True, min_periods=1).mean() In [72]: df Out[72]: col1 col2 0 0 0.5 1 1 1.0 2 2 2.0 3 3 3.0 4 4 4.0 5 5 5.0 6 6 6.0 7 7 7.0 8 8 8.0 9 9 9.0 10 10 10.0 11 11 11.0 12 12 12.0 13 13 13.0 14 14 14.0 15 15 15.0 16 16 16.0 17 17 17.0 18 18 18.0 19 19 18.5 In [78]: df['col3'] = df.col1.rolling(N, center=True, min_periods=1).mean().values[::-1] In [79]: df Out[79]: col1 col2 col3 0 0 0.5 18.5 1 1 1.0 18.0 2 2 2.0 17.0 3 3 3.0 16.0 4 4 4.0 15.0 5 5 5.0 14.0 6 6 6.0 13.0 7 7 7.0 12.0 8 8 8.0 11.0 9 9 9.0 10.0 10 10 10.0 9.0 11 11 11.0 8.0 12 12 12.0 7.0 13 13 13.0 6.0 14 14 14.0 5.0 15 15 15.0 4.0 16 16 16.0 3.0 17 17 17.0 2.0 18 18 18.0 1.0 19 19 18.5 0.5 
    • What you need! Thanks you. Only the reverse order, but this is not a problem. - Frank
    • @ IlyaLisunov, can you explain to the account of the reverse order - do you mean .shift(-1) ? - MaxU
    • take for the values ​​not the previous lines, but the following. I just opened the column df.col1[::-1].shift(-1).rolling(N, min_periods=1).mean() @MaxU - Frank
    • @ IlyaLisunov, i.e. so as in column col3 in the updated answer? - MaxU
    • Why can't I notify through the "dog" - Frank