December 2018
Beginner to intermediate
682 pages
18h 1m
English
The plot from step 19 shows quite a lot of noise and the data might be easier to interpret if it were smoothed. One common smoothing method is called the rolling average. Pandas offers the rolling method for DataFrames and groupby objects. It works analogously to the groupby method by returning an object waiting for an additional action to be performed on it. When creating it, you must pass the size of the window as the first argument, which can either be an integer or a date offset string.
In this example, we take a 90-day moving average with the date offset string 90D. The on parameter specifies the column from which the rolling window is calculated:
>>> pres_rm = pres_41_45.groupby('President', sort=False) \ .rolling('90D', ...