In [37]:
from pandas import DataFrame, Series
import pandas as pd
import plotly.plotly as py
from plotly.graph_objs import *

In [38]:
# Locate the cansim data
adultPopulation = "./Data/NBAdultPopulationCansim.csv"
vehicleRegistrations = "./Data/NBVehicleRegistrationsCansim.csv"

In [39]:
# Read in the population data
pop_df = pd.read_csv(adultPopulation)
pop_df = pop_df.drop(['GEO', 'SEX', 'AGE'], axis=1).set_index('Ref_Date')
pop_df.columns = ['New Brunswick Population 18+']
pop_df


Out[39]:
New Brunswick Population 18+
Ref_Date
1999 582202
2000 584980
2001 587572
2002 590283
2003 593141
2004 595516
2005 596595
2006 597117
2007 598986
2008 602426
2009 607099
2010 611594
2011 615859
2012 618760
2013 619439

In [40]:
# Read in the registration data
reg_df = pd.read_csv(vehicleRegistrations)
reg_df = reg_df.drop(['REGION', 'TYPE'], axis=1).set_index('Ref_Date')
reg_df.columns = ['Vehicles Registered in NB < 4500kg']
reg_df


Out[40]:
Vehicles Registered in NB < 4500kg
Ref_Date
1999 421796
2000 431284
2001 433600
2002 442703
2003 442259
2004 442592
2005 450489
2006 459671
2007 471013
2008 484797
2009 489507
2010 501145
2011 508423
2012 521925
2013 525614

In [41]:
# Merge them both together
merged = pd.merge(reg_df, pop_df, left_index=True, right_index=True)
merged


Out[41]:
Vehicles Registered in NB < 4500kg New Brunswick Population 18+
Ref_Date
1999 421796 582202
2000 431284 584980
2001 433600 587572
2002 442703 590283
2003 442259 593141
2004 442592 595516
2005 450489 596595
2006 459671 597117
2007 471013 598986
2008 484797 602426
2009 489507 607099
2010 501145 611594
2011 508423 615859
2012 521925 618760
2013 525614 619439

In [42]:
# Calculate the number of vehicles per adult
vehic_per_adult = merged.ix[:,0] / merged.ix[:,1]
vehic_per_adult


Out[42]:
Ref_Date
1999    0.724484
2000    0.737263
2001    0.737952
2002    0.749984
2003    0.745622
2004    0.743208
2005    0.755100
2006    0.769817
2007    0.786351
2008    0.804741
2009    0.806305
2010    0.819408
2011    0.825551
2012    0.843502
2013    0.848532
dtype: float64

In [43]:
# Makes it possible to plot a pandas DataFrame in plotly
def df_to_plotly(df):
    
    '''
    Coverting a Pandas Data Frame to Plotly interface
    '''
    x = df.index.values
    lines={}
    for key in df:
        lines[key]={}
        lines[key]["x"]=x
        lines[key]["y"]=df[key].values
        lines[key]["name"]=key

    #Appending all lines
    lines_plotly=[lines[key] for key in df]
    return lines_plotly

In [44]:
# Convert the data using the above function
plotly_data = df_to_plotly(merged)

In [36]:
# Plot it
py.plot(plotly_data)


Out[36]:
u'https://plot.ly/~Brideau/55'