{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-05T19:10:16.155299Z","iopub.execute_input":"2022-07-05T19:10:16.155829Z","iopub.status.idle":"2022-07-05T19:10:16.207852Z","shell.execute_reply.started":"2022-07-05T19:10:16.155699Z","shell.execute_reply":"2022-07-05T19:10:16.206836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:16.209670Z","iopub.execute_input":"2022-07-05T19:10:16.210020Z","iopub.status.idle":"2022-07-05T19:10:16.214533Z","shell.execute_reply.started":"2022-07-05T19:10:16.209988Z","shell.execute_reply":"2022-07-05T19:10:16.213730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pop_df = pd.read_csv('../input/housing-affordability-in-canada/population-by-region-1946-2022.csv')\npop_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:16.216099Z","iopub.execute_input":"2022-07-05T19:10:16.216440Z","iopub.status.idle":"2022-07-05T19:10:16.259078Z","shell.execute_reply.started":"2022-07-05T19:10:16.216408Z","shell.execute_reply":"2022-07-05T19:10:16.257991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Descriptive Statistics","metadata":{}},{"cell_type":"code","source":"pop_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:16.260921Z","iopub.execute_input":"2022-07-05T19:10:16.261589Z","iopub.status.idle":"2022-07-05T19:10:16.293070Z","shell.execute_reply.started":"2022-07-05T19:10:16.261552Z","shell.execute_reply":"2022-07-05T19:10:16.291441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pop_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:16.294417Z","iopub.execute_input":"2022-07-05T19:10:16.295006Z","iopub.status.idle":"2022-07-05T19:10:16.308245Z","shell.execute_reply.started":"2022-07-05T19:10:16.294971Z","shell.execute_reply":"2022-07-05T19:10:16.307138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualization","metadata":{}},{"cell_type":"markdown","source":"## 1. EDA on Population growth ","metadata":{}},{"cell_type":"code","source":"import plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:16.310261Z","iopub.execute_input":"2022-07-05T19:10:16.310579Z","iopub.status.idle":"2022-07-05T19:10:17.815865Z","shell.execute_reply.started":"2022-07-05T19:10:16.310540Z","shell.execute_reply":"2022-07-05T19:10:17.814254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.line(pop_df, x=\"REF_DATE\", y=\"Population estimate\", color = 'GEO', title='Line chart for Population growth in Canada')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:17.817416Z","iopub.execute_input":"2022-07-05T19:10:17.818699Z","iopub.status.idle":"2022-07-05T19:10:19.024425Z","shell.execute_reply.started":"2022-07-05T19:10:17.818647Z","shell.execute_reply":"2022-07-05T19:10:19.022870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(pop_df, x=\"REF_DATE\", y=\"Population estimate\", color=\"GEO\", title = 'Scatter plot for Population growth in Canada')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.026361Z","iopub.execute_input":"2022-07-05T19:10:19.026949Z","iopub.status.idle":"2022-07-05T19:10:19.234976Z","shell.execute_reply.started":"2022-07-05T19:10:19.026900Z","shell.execute_reply":"2022-07-05T19:10:19.233569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the above scatterplot we can interpret the following:\n* Population of Canada keeps on increasing from 1946 to 2022\n* Ontario has the highest population all the time and that increases steadily.\n* Next comes Qubec and then British Columbia and then Alberta.\n* After 1970, the growth of population in Alberta and British Columbia increased.\n* All the other places have significantly small number of population and is not increasing that much.","metadata":{}},{"cell_type":"code","source":"fig = px.bar(pop_df, x=\"REF_DATE\",  y=\"Population estimate\", color=\"GEO\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.238989Z","iopub.execute_input":"2022-07-05T19:10:19.239478Z","iopub.status.idle":"2022-07-05T19:10:19.407828Z","shell.execute_reply.started":"2022-07-05T19:10:19.239432Z","shell.execute_reply":"2022-07-05T19:10:19.406686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pop_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.409475Z","iopub.execute_input":"2022-07-05T19:10:19.410007Z","iopub.status.idle":"2022-07-05T19:10:19.425552Z","shell.execute_reply.started":"2022-07-05T19:10:19.409965Z","shell.execute_reply":"2022-07-05T19:10:19.424712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pop_df[pop_df['REF_DATE'].str.extract('^[pP].*')>0]\npop_yearly_df = pop_df.copy()\npop_yearly_df = pop_yearly_df[pop_yearly_df['REF_DATE'].str.match('^Jan.*')== True]","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.426588Z","iopub.execute_input":"2022-07-05T19:10:19.427957Z","iopub.status.idle":"2022-07-05T19:10:19.436293Z","shell.execute_reply.started":"2022-07-05T19:10:19.427925Z","shell.execute_reply":"2022-07-05T19:10:19.435106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pop_yearly_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.437941Z","iopub.execute_input":"2022-07-05T19:10:19.438516Z","iopub.status.idle":"2022-07-05T19:10:19.452702Z","shell.execute_reply.started":"2022-07-05T19:10:19.438471Z","shell.execute_reply":"2022-07-05T19:10:19.451536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npop_yearly_df = pop_yearly_df[pop_yearly_df.GEO != 'Canada']","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.454396Z","iopub.execute_input":"2022-07-05T19:10:19.455326Z","iopub.status.idle":"2022-07-05T19:10:19.463919Z","shell.execute_reply.started":"2022-07-05T19:10:19.455281Z","shell.execute_reply":"2022-07-05T19:10:19.462481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pop_yearly_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.465319Z","iopub.execute_input":"2022-07-05T19:10:19.466460Z","iopub.status.idle":"2022-07-05T19:10:19.484130Z","shell.execute_reply.started":"2022-07-05T19:10:19.466412Z","shell.execute_reply":"2022-07-05T19:10:19.483245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(pop_yearly_df, x='REF_DATE', y='Population estimate',\n             color='GEO',\n             labels={'Population estimate':'Population'}, height=400)           #\n\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.485412Z","iopub.execute_input":"2022-07-05T19:10:19.485923Z","iopub.status.idle":"2022-07-05T19:10:19.664890Z","shell.execute_reply.started":"2022-07-05T19:10:19.485891Z","shell.execute_reply":"2022-07-05T19:10:19.663995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pop_df.query(\"REF_DATE == 'Jan-22'\") #.query(\"continent == 'Europe'\")\ndf = df[df.GEO != 'Canada']\nfig = px.pie(df, values='Population estimate', names='GEO', title='Population of Canada by Province in Jan 2022')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.666331Z","iopub.execute_input":"2022-07-05T19:10:19.667004Z","iopub.status.idle":"2022-07-05T19:10:19.746756Z","shell.execute_reply.started":"2022-07-05T19:10:19.666961Z","shell.execute_reply":"2022-07-05T19:10:19.745766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The pie chart above shows the percentage of population in all Provinces and territorries as on Jan 2022**","metadata":{}},{"cell_type":"markdown","source":"# 2. EDA on Dwellings count and structural type dwelling","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/housing-affordability-in-canada/population_dwellings_count.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.751592Z","iopub.execute_input":"2022-07-05T19:10:19.752267Z","iopub.status.idle":"2022-07-05T19:10:19.770358Z","shell.execute_reply.started":"2022-07-05T19:10:19.752217Z","shell.execute_reply":"2022-07-05T19:10:19.769343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.771822Z","iopub.execute_input":"2022-07-05T19:10:19.772403Z","iopub.status.idle":"2022-07-05T19:10:19.788546Z","shell.execute_reply.started":"2022-07-05T19:10:19.772356Z","shell.execute_reply":"2022-07-05T19:10:19.787851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_df = pd.read_csv('../input/housing-affordability-in-canada/Structural-dwellings-household-size.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.790015Z","iopub.execute_input":"2022-07-05T19:10:19.790643Z","iopub.status.idle":"2022-07-05T19:10:19.808351Z","shell.execute_reply.started":"2022-07-05T19:10:19.790589Z","shell.execute_reply":"2022-07-05T19:10:19.807126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.809716Z","iopub.execute_input":"2022-07-05T19:10:19.810134Z","iopub.status.idle":"2022-07-05T19:10:19.828452Z","shell.execute_reply.started":"2022-07-05T19:10:19.810089Z","shell.execute_reply":"2022-07-05T19:10:19.827671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.829626Z","iopub.execute_input":"2022-07-05T19:10:19.830251Z","iopub.status.idle":"2022-07-05T19:10:19.845960Z","shell.execute_reply.started":"2022-07-05T19:10:19.830206Z","shell.execute_reply":"2022-07-05T19:10:19.844975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 10 missing values in the 'Average household size' column that I missed. I'm going to drop those rows.\n\ntrain.describe(include=['object']).T.style    \n\n","metadata":{}},{"cell_type":"code","source":"rows_with_nan = str_dwell_df[str_dwell_df['Average household size'].isna()]\nrows_with_nan","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.847309Z","iopub.execute_input":"2022-07-05T19:10:19.847880Z","iopub.status.idle":"2022-07-05T19:10:19.865000Z","shell.execute_reply.started":"2022-07-05T19:10:19.847828Z","shell.execute_reply":"2022-07-05T19:10:19.863689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_df = str_dwell_df.dropna()     #Dropped all the rows with NaN","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.866395Z","iopub.execute_input":"2022-07-05T19:10:19.867278Z","iopub.status.idle":"2022-07-05T19:10:19.877212Z","shell.execute_reply.started":"2022-07-05T19:10:19.867236Z","shell.execute_reply":"2022-07-05T19:10:19.876370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.878188Z","iopub.execute_input":"2022-07-05T19:10:19.879036Z","iopub.status.idle":"2022-07-05T19:10:19.893229Z","shell.execute_reply.started":"2022-07-05T19:10:19.878972Z","shell.execute_reply":"2022-07-05T19:10:19.892327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.894681Z","iopub.execute_input":"2022-07-05T19:10:19.895134Z","iopub.status.idle":"2022-07-05T19:10:19.928684Z","shell.execute_reply.started":"2022-07-05T19:10:19.895101Z","shell.execute_reply":"2022-07-05T19:10:19.927502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_df.describe(include=['object']).T.style    \n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:19.930263Z","iopub.execute_input":"2022-07-05T19:10:19.931258Z","iopub.status.idle":"2022-07-05T19:10:20.021600Z","shell.execute_reply.started":"2022-07-05T19:10:19.931200Z","shell.execute_reply":"2022-07-05T19:10:20.020397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(str_dwell_df, x=\"Structural type of dwelling\",  y=\"Total - Household\", color = 'GEO')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:20.023044Z","iopub.execute_input":"2022-07-05T19:10:20.023928Z","iopub.status.idle":"2022-07-05T19:10:20.910293Z","shell.execute_reply.started":"2022-07-05T19:10:20.023882Z","shell.execute_reply":"2022-07-05T19:10:20.909109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf = str_dwell_df[str_dwell_df['Structural type of dwelling'] != 'Total - Structural type of dwelling']\n\nfig = px.pie(df, values='Total - Household', names='Structural type of dwelling', title='Structural type of dwelling - 2022')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:20.912086Z","iopub.execute_input":"2022-07-05T19:10:20.913082Z","iopub.status.idle":"2022-07-05T19:10:20.980631Z","shell.execute_reply.started":"2022-07-05T19:10:20.913032Z","shell.execute_reply":"2022-07-05T19:10:20.979600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the above figure we can interpret the following:\n* 50% of dwelling is Single deteched house\n* The next most common type of dwelling is Apartment in a building that has fewer than five storeys","metadata":{}},{"cell_type":"code","source":"str_dwell_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:20.982309Z","iopub.execute_input":"2022-07-05T19:10:20.983507Z","iopub.status.idle":"2022-07-05T19:10:21.002897Z","shell.execute_reply.started":"2022-07-05T19:10:20.983464Z","shell.execute_reply":"2022-07-05T19:10:21.000793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npd.set_option('display.max_rows', None)\nstr_dwell_df['GEO'].value_counts()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.004541Z","iopub.execute_input":"2022-07-05T19:10:21.005990Z","iopub.status.idle":"2022-07-05T19:10:21.018819Z","shell.execute_reply.started":"2022-07-05T19:10:21.005942Z","shell.execute_reply":"2022-07-05T19:10:21.017474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# list of Province and territories in Canada\n\nprov_terr = ['Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Nova Scotia','Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan', 'Newfoundland and Labrador',\n                'Northwest Territories','Nunavut','Yukon']\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.020375Z","iopub.execute_input":"2022-07-05T19:10:21.020746Z","iopub.status.idle":"2022-07-05T19:10:21.027921Z","shell.execute_reply.started":"2022-07-05T19:10:21.020717Z","shell.execute_reply":"2022-07-05T19:10:21.026648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"str_dwell_prov_df = str_dwell_df[str_dwell_df['GEO'].isin(prov_terr)]","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.030429Z","iopub.execute_input":"2022-07-05T19:10:21.030936Z","iopub.status.idle":"2022-07-05T19:10:21.039237Z","shell.execute_reply.started":"2022-07-05T19:10:21.030893Z","shell.execute_reply":"2022-07-05T19:10:21.038028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_prov_df.GEO.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.042749Z","iopub.execute_input":"2022-07-05T19:10:21.043537Z","iopub.status.idle":"2022-07-05T19:10:21.056754Z","shell.execute_reply.started":"2022-07-05T19:10:21.043499Z","shell.execute_reply":"2022-07-05T19:10:21.055459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_prov_df.head(9)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.058230Z","iopub.execute_input":"2022-07-05T19:10:21.058924Z","iopub.status.idle":"2022-07-05T19:10:21.080086Z","shell.execute_reply.started":"2022-07-05T19:10:21.058879Z","shell.execute_reply":"2022-07-05T19:10:21.078871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(str_dwell_prov_df, x=\"Structural type of dwelling\", y=\"Total - Household\",\n             color='GEO', barmode='group',height=700)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.081662Z","iopub.execute_input":"2022-07-05T19:10:21.082392Z","iopub.status.idle":"2022-07-05T19:10:21.185323Z","shell.execute_reply.started":"2022-07-05T19:10:21.082346Z","shell.execute_reply":"2022-07-05T19:10:21.184224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(str_dwell_prov_df, x=\"GEO\", y=\"Total - Household\",\n             color='Structural type of dwelling', barmode='group')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.186804Z","iopub.execute_input":"2022-07-05T19:10:21.187145Z","iopub.status.idle":"2022-07-05T19:10:21.276683Z","shell.execute_reply.started":"2022-07-05T19:10:21.187115Z","shell.execute_reply":"2022-07-05T19:10:21.275874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig = px.bar(str_dwell_prov_df[str_dwell_prov_df.GEO == 'Ontario'], x=\"GEO\", y=\"Total - Household\",\n             color='Structural type of dwelling', barmode='group')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.277983Z","iopub.execute_input":"2022-07-05T19:10:21.278293Z","iopub.status.idle":"2022-07-05T19:10:21.379978Z","shell.execute_reply.started":"2022-07-05T19:10:21.278266Z","shell.execute_reply":"2022-07-05T19:10:21.378636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(str_dwell_prov_df, x=\"Structural type of dwelling\", y=\"Total - Household\",\n             color='GEO')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.381436Z","iopub.execute_input":"2022-07-05T19:10:21.382615Z","iopub.status.idle":"2022-07-05T19:10:21.496590Z","shell.execute_reply.started":"2022-07-05T19:10:21.382570Z","shell.execute_reply":"2022-07-05T19:10:21.495304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_dwell_prov_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.498550Z","iopub.execute_input":"2022-07-05T19:10:21.499254Z","iopub.status.idle":"2022-07-05T19:10:21.517073Z","shell.execute_reply.started":"2022-07-05T19:10:21.499195Z","shell.execute_reply":"2022-07-05T19:10:21.516014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig = px.bar(str_dwell_prov_df, x=\"Structural type of dwelling\", y=\"Total - Household\", facet_row=\"GEO\")\n\nfig = px.bar(str_dwell_prov_df, x=\"GEO\", y=\"Total - Household\", facet_col=\"Structural type of dwelling\", facet_col_wrap=2, height = 1500)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.518704Z","iopub.execute_input":"2022-07-05T19:10:21.519080Z","iopub.status.idle":"2022-07-05T19:10:21.808699Z","shell.execute_reply.started":"2022-07-05T19:10:21.519046Z","shell.execute_reply":"2022-07-05T19:10:21.807876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. EDA on CPI ","metadata":{}},{"cell_type":"code","source":"CPI_df = pd.read_csv('../input/housing-affordability-in-canada/CPI-inflation-by-region-1914-202.csv')\nCPI_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:21.813337Z","iopub.execute_input":"2022-07-05T19:10:21.814263Z","iopub.status.idle":"2022-07-05T19:10:22.047197Z","shell.execute_reply.started":"2022-07-05T19:10:21.814198Z","shell.execute_reply":"2022-07-05T19:10:22.046371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CPI_all_df = CPI_df[CPI_df['Products and product groups'] == 'All-items']\nCPI_all_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:22.048389Z","iopub.execute_input":"2022-07-05T19:10:22.049052Z","iopub.status.idle":"2022-07-05T19:10:22.072895Z","shell.execute_reply.started":"2022-07-05T19:10:22.049008Z","shell.execute_reply":"2022-07-05T19:10:22.071701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CPI_all_df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:17:35.269801Z","iopub.execute_input":"2022-07-05T19:17:35.270935Z","iopub.status.idle":"2022-07-05T19:17:35.295880Z","shell.execute_reply.started":"2022-07-05T19:17:35.270893Z","shell.execute_reply":"2022-07-05T19:17:35.294702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Return a Series containing counts of unique values in column UOM\nCPI_all_df.UOM.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:10:22.431443Z","iopub.execute_input":"2022-07-05T19:10:22.432164Z","iopub.status.idle":"2022-07-05T19:10:22.441344Z","shell.execute_reply.started":"2022-07-05T19:10:22.432119Z","shell.execute_reply":"2022-07-05T19:10:22.440318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Checked how many unique values are in UOM. Usually when comparing the CPI we have to use the same reference year.\nHere only one unique value is present and the reference year is 2002 = 100, So we can use CPI data from 1914 - 2021 for comparing.","metadata":{}},{"cell_type":"code","source":"fig = px.line(CPI_all_df, x=\"REF_DATE\", y=\"CPI\", color = 'GEO', title='Line chart for CPI in Canada')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:33:17.572419Z","iopub.execute_input":"2022-07-05T19:33:17.573047Z","iopub.status.idle":"2022-07-05T19:33:17.725956Z","shell.execute_reply.started":"2022-07-05T19:33:17.573000Z","shell.execute_reply":"2022-07-05T19:33:17.725139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After 1971 only provincial data is available. So plotting the CPI for year(1971 - 2021)","metadata":{}},{"cell_type":"code","source":"fig = px.line( CPI_all_df[ (CPI_all_df.REF_DATE > 1970) & ( CPI_all_df['GEO'].isin(prov_terr) )], x=\"REF_DATE\", y=\"CPI\", color = 'GEO', title='Line chart for CPI in Canadian Provinces and territorries(1971-2021)')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:34:24.830436Z","iopub.execute_input":"2022-07-05T19:34:24.830822Z","iopub.status.idle":"2022-07-05T19:34:24.919461Z","shell.execute_reply.started":"2022-07-05T19:34:24.830766Z","shell.execute_reply":"2022-07-05T19:34:24.918340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.line( CPI_all_df[ (CPI_all_df.REF_DATE > 1970) & ( CPI_all_df['GEO']=='Canada' )], x=\"REF_DATE\", y=\"CPI\", color = 'GEO', title='Line chart for CPI in Canada(1971-2021)')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:34:38.986823Z","iopub.execute_input":"2022-07-05T19:34:38.987215Z","iopub.status.idle":"2022-07-05T19:34:39.049546Z","shell.execute_reply.started":"2022-07-05T19:34:38.987184Z","shell.execute_reply":"2022-07-05T19:34:39.048373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar( CPI_all_df[ (CPI_all_df.REF_DATE > 1970) & ( CPI_all_df['GEO'] =='Canada' )], x=\"REF_DATE\", y=\"CPI\", color = 'GEO', title='Bar chart for CPI in Canada(1971-2021)')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:52:44.821177Z","iopub.execute_input":"2022-07-05T19:52:44.821607Z","iopub.status.idle":"2022-07-05T19:52:44.881360Z","shell.execute_reply.started":"2022-07-05T19:52:44.821572Z","shell.execute_reply":"2022-07-05T19:52:44.880258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar( CPI_all_df[ (CPI_all_df.REF_DATE > 2010) & ( CPI_all_df['GEO'].isin(prov_terr) )], x=\"REF_DATE\", y=\"CPI\", color = 'GEO', title='CPI in Canada(2010-2021)',barmode = 'group')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:59:42.153187Z","iopub.execute_input":"2022-07-05T19:59:42.153580Z","iopub.status.idle":"2022-07-05T19:59:42.251945Z","shell.execute_reply.started":"2022-07-05T19:59:42.153548Z","shell.execute_reply":"2022-07-05T19:59:42.250878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar( CPI_df[ (CPI_df.REF_DATE == 2021) & ( CPI_df['GEO']=='Canada' )], x=\"Products and product groups\", y=\"CPI\", title='CPI for different products in Canada 2021', height = 1000)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:50:47.585382Z","iopub.execute_input":"2022-07-05T19:50:47.585776Z","iopub.status.idle":"2022-07-05T19:50:47.651348Z","shell.execute_reply.started":"2022-07-05T19:50:47.585741Z","shell.execute_reply":"2022-07-05T19:50:47.650330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Top inflated products in Canada in 2021","metadata":{}},{"cell_type":"code","source":"CPI_df[ (CPI_df.REF_DATE == 2021) & ( CPI_df['GEO']=='Canada' ) & ( CPI_df.CPI > 200 ) ].sort_values('CPI', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:49:35.586920Z","iopub.execute_input":"2022-07-05T19:49:35.587931Z","iopub.status.idle":"2022-07-05T19:49:35.612543Z","shell.execute_reply.started":"2022-07-05T19:49:35.587888Z","shell.execute_reply":"2022-07-05T19:49:35.611316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* CPI is highest for 'Home owners home and mortgage insurance'(275.3)\n* then comes 'Other tobacco products and smokers' supplies', 'Water,'Tobacco products and smokers' supplies', Cigarettes\n* After that 'Fuel oil and other fuels'","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CPI_df['UOM'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:35:41.742116Z","iopub.execute_input":"2022-07-05T19:35:41.742516Z","iopub.status.idle":"2022-07-05T19:35:41.757047Z","shell.execute_reply.started":"2022-07-05T19:35:41.742481Z","shell.execute_reply":"2022-07-05T19:35:41.756293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CPI_df[ CPI_df['UOM'] == '2013=100' ].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:00:03.536089Z","iopub.execute_input":"2022-07-05T20:00:03.537142Z","iopub.status.idle":"2022-07-05T20:00:03.563319Z","shell.execute_reply.started":"2022-07-05T20:00:03.537081Z","shell.execute_reply":"2022-07-05T20:00:03.562221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"References:\n1. https://plotly.com/python/basic-charts/\n2. https://plotly.com/python/facet-plots/\n3. https://www.bls.gov/cpi/factsheets/cpi-math-calculations.pdf","metadata":{}}]}