Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_16/rolling_averages - to help clean data and see growth and trends (GDP) (done).ipynb
1904 views
Kernel: Python 3

Greatest Economy in History?

from IPython.display import Image Image('assets/trumptweet.PNG')
Image in a Jupyter notebook
%matplotlib inline import pandas as pd import matplotlib.pyplot as plt

Read in the GDP Data

df = pd.read_csv('data/GDP.csv') df['DATE'] = pd.to_datetime(df['DATE']) df.tail()
df.set_index('DATE', inplace=True) df.plot() plt.title('Real GDP in Constant 2009 dollars, Seasonally Adjusted') plt.axvline('2017-01-01', color='r')
<matplotlib.lines.Line2D at 0x1077baa90>
Image in a Jupyter notebook

Lets look at in Percent Differences

df['gdp_pct_change'] = df['GDPC1'].pct_change() * 100 df['gdp_pct_change'].plot() plt.title('GDP: Percent Change Q/Q') plt.axvline('2017-01-01',color='r')
<matplotlib.lines.Line2D at 0x10dc11080>
Image in a Jupyter notebook

Lets zoom in

zoom = df['1998':] zoom['gdp_pct_change'].plot() plt.title('GDP: Percent Change Q/Q') plt.axvline(x='2017-01-01', color='r')
<matplotlib.lines.Line2D at 0x1158cdeb8>
Image in a Jupyter notebook

Lets look at this on 4 quarter rolling average basis

zoom['mvg_avg'] = zoom['gdp_pct_change'].rolling(4).mean() zoom['mvg_avg'].plot() plt.title('GDP: Percent Change Q/Q, 4Q rolling average') plt.axvline('2017-01-01', color='red')
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy """Entry point for launching an IPython kernel.
<matplotlib.lines.Line2D at 0x1158967b8>
Image in a Jupyter notebook

What about momentum?

zoom['momentum'] = zoom['gdp_pct_change'].diff() * 100 zoom['momentum'].plot() plt.title('Growth Momentum') plt.axvline('2017-01-01', color='r')
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy """Entry point for launching an IPython kernel.
<matplotlib.lines.Line2D at 0x115a23a90>
Image in a Jupyter notebook
zoom['momentum'].rolling(4).mean().plot() plt.title('Growth Momentum: 4q rolling average') plt.axvline('2017-01-01', color='r')
<matplotlib.lines.Line2D at 0x115b3b898>
Image in a Jupyter notebook