I have a pandas pivot table that was previously shifted and now looks like this:
pivot
A B C D E
0 5.3 5.1 3.5 4.2 4.5
1 5.3 4.1 3.5 4.2 NaN
2 4.3 4.1 3.5 NaN NaN
3 4.3 4.1 NaN NaN NaN
4 4.3 NaN NaN NaN NaN
I'm trying to calculate a rolling average with a variable window (in this case 3 and 4 periods) over the inverse diagonal iterating over every column and trying to store that value in a new dataframe, which would look like this:
expected_df with a 3 periods window
A B C D E
0 4.3 4.1 3.5 4.2 4.5
expected_df with a 4 periods window
A B C D E
0 4.5 4.3 3.5 4.2 4.5
So far, I tried to subset the original pivot table and create a different dataframe that only contains the specified window values for each column, to then calculate the average, like this:
subset
A B C D E
0 4.3 4.1 3.5 4.2 4.5
1 4.3 4.1 3.5 4.2 NaN
2 4.3 4.1 3.5 NaN NaN
For this, I tried to build the following for loop:
df2 = pd.DataFrame()
size = pivot.shape[0]
window = 3
for i in range(size):
df2[i] = pivot.iloc[size-window-i:size-i,i]
Which does not work even when this pivot.iloc[size-window-i:size-i,i]
does return the values I need when I manually pass in the indexes, but in the for loop, it misses the first value of the second column and so on:
df2
A B C D E
0 4.3 NaN NaN NaN NaN
1 4.3 4.1 NaN NaN NaN
2 4.3 4.1 3.5 NaN NaN
Does anyone have a good idea on how to calculate the moving average or on how to fix the for loop part? Thanks in advance for your comments.