Created
February 17, 2020 19:29
-
-
Save pskifast/3729e5a859aeb3cbeeb74cf0536ef97a to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Segmenting and Clustering Neighborhoods in Toronto" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# importing required libraries\n", | |
"\n", | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import seaborn as sns\n", | |
"%matplotlib inline\n", | |
"\n", | |
"!pip install lxml\n", | |
"\n", | |
"print('All imported!')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Section 1: Web scraping Wikipedia HTML tables\n", | |
"\n", | |
"web site: https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Parsing the tables of the target webpage\n", | |
"data = pd.read_html('http://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M', header=0)\n", | |
"df = data[0]\n", | |
"\n", | |
"# Changing column names\n", | |
"df.columns = ['PostalCode', 'Borough', 'Neighborhood']\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Section 2: Performing required operations" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Data cleaning per the instructions\n", | |
"df.drop(df[df.Borough == 'Not assigned'].index, inplace=True)\n", | |
"df.loc[df.Neighborhood == 'Not assigned', 'Neighborhood'] = df['Borough']\n", | |
"df.reset_index(drop=True, inplace=True)\n", | |
"df = df.groupby('PostalCode').agg({'Borough':'first','Neighborhood': ', '.join}).reset_index()\n", | |
"\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Section 3: Verifying results" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Testing if grouping works, by comparing with table given in the instructions**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df.loc[df['PostalCode'] == 'M9V']" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Printing number of rows of the resulting table**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"rows = df.shape\n", | |
"print('Final table has',df.shape[0], 'rows.')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Section 4: Adding coordinates" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# geocoder module is not working; using the csv file instead\n", | |
"# importing coordinates\n", | |
"coordinates = pd.DataFrame()\n", | |
"coordinates = pd.read_csv('Geospatial_Coordinates.csv',',')\n", | |
"\n", | |
"coordinates.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# populating original table with coordinates based on Postal Code\n", | |
"# renaiming columns in order to enable semaless merging\n", | |
"coordinates.columns = ['PostalCode','Latitude','Longitude']\n", | |
"# merging the two dataframes\n", | |
"df1 = pd.merge(df, coordinates, on=['PostalCode'])\n", | |
"df1.head()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python", | |
"language": "python", | |
"name": "conda-env-python-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment