Moving average 2 day span using Pandas

Viewed 169

I have a dataframe, df, that I would like to calculate the moving average for the next 2 days, with a window of 5. I then wish to store the results in a variable within Python:

 date                count

 08052020            50
 08152020            10
 08252020            30
 09052020            10
 09152020            15
 09252020            5

This is what I am doing:

 count = df['new'] = df['count'].rolling(2).mean()
 print(count)

Any insight is appreciated.

Updated Desired output:

 count = [23, 14]
       

         
1 Answers

I think you need:

df['new'] = df['count'].rolling(5).mean()

count = df["new"].dropna().to_list()

print(count)

Output:

[23.0, 14.0]
Related