There's more...

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', on='End Date')['Approving']
.mean()
>>> pres_rm.head()
President End Date George Bush 1989-01-26 51.000000 1989-02-27 55.500000 1989-03-02 57.666667 1989-03-10 58.750000 1989-03-13 58.200000 Name: Approving, dtype: float64

From here, we can restructure the data so that it looks similar to the output from step 23 with the unstack method, and then make our plot:

>>> styles = ['-.', '-', ':', '-', ':']
>>> colors = [.9, .3, .7, .3, .9]
>>> color = cm.Greys(colors)
>>> title='90 Day Approval Rating Rolling Average'
>>> plot_kwargs = dict(figsize=(16,6), style=styles,
color = color, title=title)
>>> correct_col_order = pres_41_45.President.unique()

>>> pres_rm.unstack('President')[correct_col_order].plot(**plot_kwargs)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset