In [1]:
import pandas as pd
import quandl as ql
import matplotlib as plt
% matplotlib inline
In [2]:
api_key=open('api_key.txt').read()
In [3]:
df=ql.get('FMAC/HPI_AK', authtoken=api_key)
df.plot()
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3c928a0630>
In [4]:
us_states=pd.read_html('https://simple.wikipedia.org/wiki/List_of_U.S._states')
for abbv in us_states[0][1][1:]:
    print('FMAC/HPI_' + str(abbv))
FMAC/HPI_AL
FMAC/HPI_AK
FMAC/HPI_AZ
FMAC/HPI_AR
FMAC/HPI_CA
FMAC/HPI_CO
FMAC/HPI_CT
FMAC/HPI_DE
FMAC/HPI_FL
FMAC/HPI_GA
FMAC/HPI_HI
FMAC/HPI_ID
FMAC/HPI_IL
FMAC/HPI_IN
FMAC/HPI_IA
FMAC/HPI_KS
FMAC/HPI_KY
FMAC/HPI_LA
FMAC/HPI_ME
FMAC/HPI_MD
FMAC/HPI_MA
FMAC/HPI_MI
FMAC/HPI_MN
FMAC/HPI_MS
FMAC/HPI_MO
FMAC/HPI_MT
FMAC/HPI_NE
FMAC/HPI_NV
FMAC/HPI_NH
FMAC/HPI_NJ
FMAC/HPI_NM
FMAC/HPI_NY
FMAC/HPI_NC
FMAC/HPI_ND
FMAC/HPI_OH
FMAC/HPI_OK
FMAC/HPI_OR
FMAC/HPI_PA
FMAC/HPI_RI
FMAC/HPI_SC
FMAC/HPI_SD
FMAC/HPI_TN
FMAC/HPI_TX
FMAC/HPI_UT
FMAC/HPI_VT
FMAC/HPI_VA
FMAC/HPI_WA
FMAC/HPI_WV
FMAC/HPI_WI
FMAC/HPI_WY
In [5]:
for abbv in us_states[0][1][1:]:
    query = "FMAC/HPI_"+str(abbv)
    df = ql.get(query, authtoken=api_key)
    df.rename(columns={'Value':str(abbv)} , inplace=True)
    df.plot()
/home/ekansh/.local/lib/python3.5/site-packages/matplotlib/pyplot.py:537: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)
In [6]:
def fifty_states():
    fifty_states=pd.read_html('https://simple.wikipedia.org/wiki/List_of_U.S._states')
    return fifty_states[0][1][1:]

def get_initial_state_data():
    states=fifty_states()
    main_df = pd.DataFrame()

    for abbv in states:
        query = "FMAC/HPI_"+str(abbv)
        df=quandl.get(query, authtoken=api_key)
        df.rename(columns={'Value':str(abbv)} , inplace=True)
        if main_df.empty:
            main_df=df
        else:
            main_df = main_df.join(df)
    main_df.plot()
    save_file=open('fifty_states.pickle','wb')
    pickle.dump(main_df,save_file)
    save_file.close()
In [7]:
import pickle
# using pickle of python
saved_file=open('fifty_states.pickle','rb')
HPI_data=pickle.load(saved_file)
HPI_data.plot(legend=None)
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3c8b415668>
In [8]:
#using pickle for pandas
HPI_data.to_pickle('fifty_states_pandas.pickle')
HPI_data2=pd.read_pickle('fifty_states_pandas.pickle')
HPI_data2.head()
Out[8]:
AL AK AZ AR CA CO CT DE FL GA ... SD TN TX UT VT VA WA WV WI WY
Date
1975-01-31 35.453384 34.385997 28.940587 36.845816 15.696667 19.647452 24.493861 27.256874 31.036368 30.746130 ... 37.754067 32.171503 32.626112 24.593496 26.620695 27.901501 17.478975 41.006639 28.115545 31.663002
1975-02-28 35.666686 34.910701 29.476038 37.185864 15.747528 19.875307 25.032651 27.249036 32.252269 30.471590 ... 37.337158 32.222504 32.958338 24.947068 26.937920 28.192244 17.544439 42.051072 28.490112 32.157887
1975-03-31 35.915007 35.446987 29.980514 37.482491 15.936916 20.102116 25.440129 27.272794 34.035890 30.358694 ... 36.912611 32.312085 33.546541 25.262533 27.244899 28.407625 17.652704 43.139021 28.861735 32.678700
1975-04-30 36.216014 36.002154 30.372962 37.722278 16.249400 20.315570 25.669265 27.372090 36.222625 30.421525 ... 36.489475 32.439126 34.398979 25.510183 27.525197 28.570828 17.785996 44.260809 29.197658 33.211169
1975-05-31 36.494630 36.599658 30.577860 37.947634 16.509738 20.494853 25.738778 27.575029 36.457310 30.530216 ... 36.102422 32.585102 34.651415 25.671483 27.771740 28.712080 17.910147 45.388703 29.476274 33.718382

5 rows × 50 columns

In [9]:
from matplotlib import style
style.use('ggplot')
In [10]:
def get_initial_state_data():
    states = fifty_states()
    main_df = c

    for abbv in states:
        query = "FMAC/HPI_"+str(abbv)
        df = quandl.get(query, authtoken=api_key)
        df.rename(columns = {'Value': str(abbv)}, inplace=True)
        df = df.pct_change()

        if main_df.empty:
            main_df = df
        else:
            main_df = main_df.join(df)
    main_df.plot()
    save_file = open('fifty_states_pct_change.pickle', 'wb')
    pickle.dump(main_df, save_file)
    save_file.close()
HPI_data = pd.read_pickle('fifty_states_pct_change.pickle')
HPI_data.plot(legend=None)
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3c8b6357b8>
In [11]:
def get_initial_state_data():
    states=fifty_states()
    main_df = pd.DataFrame()
    for abbv in states:
        query = "FMAC/HPI_"+str(abbv)
        df = quandl.get(query, authtoken=api_key)
        df.rename(columns={'Value':str(abbv)} , inplace=True)
        df[abbv]=(df[abbv]-df[abbv][0])/df[abbv][0]*100
        if main_df.empty:
            main_df = df
        else:
            main_df = main_df.join(df)
    main_df.plot()
    save_file=open('fifty_states_pct_change_5.pickle','wb')
    pickle.dump(main_df,save_file)
    save_file.close()
In [12]:
HPI_data=pd.read_pickle('fifty_states_pct_change_5.pickle')
HPI_data.plot(legend=None)
Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3c8b329da0>