Home > Mobile >  How to avoid misrepresented data by 3 days in the rolling mean graph in python?
How to avoid misrepresented data by 3 days in the rolling mean graph in python?

Time:02-11

I calculated 7day_rolling_avg because irregularities in covid data by this code:

covid_df['7day_rolling_avg'] = covid_df['New cases'].rolling(7).mean()

But, by plotting a graph on top of each other new graph can be seen misrepresented by 3-4 days.

Code:

covid_df[covid_df["Country"]=="India"]['New cases'].plot()
covid_df[covid_df["Country"]=="India"]['7day_rolling_avg'].plot()
plt.legend()
plt.show()

Output: enter image description here

Can someone give me any idea how can I fix it?

CodePudding user response:

use center=True in rolling

covid_df['7day_rolling_avg'] = covid_df['New cases'].rolling(7, center=True).mean()
  • Related