Consider that I have a dataframe of 10 rows having two columns A and B as follows:
A B
0 21 6
1 87 0
2 87 0
3 25 0
4 25 0
5 14 0
6 79 0
7 70 0
8 54 0
9 35 0
In excel, I can calculate rolling meanhow this is, excluding the first line:

How to do it in pandas?
Here is what I tried:
import pandas as pd
df = pd.read_clipboard()
for i in range(1, len(df)):
df.loc[i, 'B'] = df[['A', 'B']].loc[i-1].mean()
This gives me the desired result, corresponding to excel. But is there a better pandas way to do this? I tried to use expanding, but rollingdid not give the desired result.
Abbas source
share