I am trying find something resembles a moving average but excludes the previous set of values Below snapshot of excel sheet shows the average with a window of 2.
I tried using rolling and expand command in pandas but they include the previous value. Are there any other methods that can be used from pandas
CodePudding user response:
An easy way of doing this is to convert the column to a list and then calculate the moving averages using a loop (or more precisely a "list comprehension").
# Creating a DataFrame (like the one you have)
values_col = [i * 10 for i in range(10)]
df = pd.DataFrame({'values': values_col})
window_size = 2
hop_size = 2
n = len(df.index)
values_moving_average = [sum(df['values'].tolist()[i - window_size : i]) / window_size for i in range(window_size, n 1, hop_size)]
Then you will have the moving averages you wanted in values_moving_average list:
# result
[5.0, 25.0, 45.0, 65.0, 85.0]
Here, window_size
is the size of the window, and hop_size is a variable you can use specify the distance between the first indices of two consecutive windows. For example if hop_size = 1
, then we will get the average of the following windows in a list:
(0, 10), (10, 20), (20, 30), ..., (80, 90)
But, with hop_size = 2
, we will have the following windows:
(0, 10), (20, 30), (40, 50), ..., (80, 90)