I have a dataframe such as:
A
27.00
18.00
15.00
7.50
5.00
4.00
3.00
1.50
1.00
now I want to calculate average and standard deviation per 5 rows from bottom to top and set it at a above row as an additional column such as:
A B(avg) C(standard deviation)
27.00 9.90 6.24899992
18.00 6.90 4.827007354
15.00 4.20 2.252776065
7.50 2.90 1.673320053
5.00
4.00
3.00
1.50
1.00
9.90 = average-4.00, 5.00, 7.50, 15.00, 18.00
6.24899992 = standard deviation-4.00, 5.00, 7.50, 15.00, 18.00
How can I achieve that?
Use rolling and shift a result 5 row up
df[['B','С']] = df.rolling(5)['A'].agg(('mean','std')).shift(-5)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments