Assume, you have a dataframe and the result for rolling window size 3 calculation is,
Average of rolling window is: Id Age Mark 0 NaN NaN NaN 1 1.5 12.0 85.0 2 2.5 13.0 80.0 3 3.5 13.5 82.5 4 4.5 31.5 90.0 5 5.5 60.0 87.5
To solve this, we will follow the below approach −
Solution
Define a dataframe
Apply df.rolling(window=2).mean() to calculate average of rolling window size 3 is
df.rolling(window=2).mean()
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, 4, 5,6], "Age":[12, 12, 14, 13, 50,70], "Mark":[80, 90, 70, 95, 85,90], }) print("Dataframe is:\n",df) print("Average of rolling window is:\n",df.rolling(window=2).mean())
Output
Dataframe is: Id Age Mark 0 1 12 80 1 2 12 90 2 3 14 70 3 4 13 95 4 5 50 85 5 6 70 90 Average of rolling window is: Id Age Mark 0 NaN NaN NaN 1 1.5 12.0 85.0 2 2.5 13.0 80.0 3 3.5 13.5 82.5 4 4.5 31.5 90.0 5 5.5 60.0 87.5