I have a simple pandas.DataFrame
A
0 1
1 2
2 3
3 4
4 5
5 6
I am trying to calculate the standard deviation on a rolling basis, all starting from the first row. So it would be standard deviation from rows 1-4, then rows 1-5, then rows 1-6, etc. I could easily do this iteratively but I'd prefer to do it with pandas functionality.
The final output should be
A Standard Deviation
0 1
1 2
2 3
3 4 1.581
4 5 1.870
5 6 2.160
Is there an appropriate method to do so?
CodePudding user response:
You can use expanding.std
:
df['A'].expanding(min_periods=4).std()
Output:
0 NaN
1 NaN
2 NaN
3 1.290994
4 1.581139
5 1.870829
Name: A, dtype: float64