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)