Calculate average and standard deviation per 5 rows in a pandas dataframe

Mono Neu Elogy

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?

splash58

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.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas dataframe groupby to calculate population standard deviation

Pandas standard deviation on one column for subset of rows

How can I calculate standard deviation in pandas dataframe?

Add new rows to calculate the sum and average from exiting pandas dataframe

How Do I calculate standard deviation of pandas dataframe in python

How to calculate standard deviation with pandas for each row?

Pandas Group By Multiple Colums and Calculate Standard Deviation

Pandas DataFrame and numpy standard deviation are different

How to group pandas dataframe and calculate statistical summary (mean and standard deviation) without current row value?

SQL STDEVP as analytical function (calculate standard deviation per account)

How to calculate standard deviation per row?

calculate average and maximum value for subset of rows in pandas dataframe

C++ calculate average and standard deviation

Group a dataframe and calculate normalised standard deviation by group?

Pandas Dataframe grouping and standard deviation

How to calculate average and standard deviation from the same range in excel

Pandas moving average using a standard deviation in Python

How to calculate standard deviation and average values of RDD[Long]?

Standard deviation and mean of complete pandas dataframe

calculate standard deviation of datetime rows for specefic column and save to new column in pandas

Calculate average and standard deviation for pre defined number of values substituting missing rows with zeros

Calculate Weighted Average and Weighted Standard Deviation in DAX

How to calculate moving average for each subsets of rows in pandas dataframe?

Calculate standard deviation for intervals in dataframe column

How to calculate mean/variance/standard deviation per index of array?

Standard deviation of average events per ID in R

Using pandas DataFrame, how to calculate the standard deviation between first row and each row after?

How to Calculate the Average and Standard Deviation of Two Ranges

Calculate average between mean and 1st standard deviation