Skip to content

Instantly share code, notes, and snippets.

@tossolini
Created July 5, 2019 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tossolini/f76974a91d1f47afb3a9e7ca6c7399de to your computer and use it in GitHub Desktop.
Save tossolini/f76974a91d1f47afb3a9e7ca6c7399de to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: beautifulsoup4 in /home/jupyterlab/conda/lib/python3.6/site-packages (4.7.1)\n",
"Requirement already satisfied: soupsieve>=1.2 in /home/jupyterlab/conda/lib/python3.6/site-packages (from beautifulsoup4) (1.8)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"pip install beautifulsoup4\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: lxml in /home/jupyterlab/conda/lib/python3.6/site-packages (4.3.0)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"pip install lxml"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: requests in /home/jupyterlab/conda/lib/python3.6/site-packages (2.22.0)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/jupyterlab/conda/lib/python3.6/site-packages (from requests) (1.24.3)\n",
"Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/jupyterlab/conda/lib/python3.6/site-packages (from requests) (3.0.4)\n",
"Requirement already satisfied: idna<2.9,>=2.5 in /home/jupyterlab/conda/lib/python3.6/site-packages (from requests) (2.8)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/lib/python3.6/site-packages (from requests) (2019.6.16)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"pip install requests"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting bs4\n",
" Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz\n",
"Requirement already satisfied: beautifulsoup4 in /home/jupyterlab/conda/lib/python3.6/site-packages (from bs4) (4.7.1)\n",
"Requirement already satisfied: soupsieve>=1.2 in /home/jupyterlab/conda/lib/python3.6/site-packages (from beautifulsoup4->bs4) (1.8)\n",
"Building wheels for collected packages: bs4\n",
" Building wheel for bs4 (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /home/jupyterlab/.cache/pip/wheels/a0/b0/b2/4f80b9456b87abedbc0bf2d52235414c3467d8889be38dd472\n",
"Successfully built bs4\n",
"Installing collected packages: bs4\n",
"Successfully installed bs4-0.0.1\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"pip install bs4\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from bs4 import BeautifulSoup\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"\n",
"with open('/resources/data/Toronto_wiki.html') as html_file:\n",
" soup = BeautifulSoup(html_file, 'lxml')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Files in '/resources': ['First Notebook.ipynb', 'Tutorial #1 - Get Data.ipynb', '.ka_workbench', 'labs', '.jupyterlab', 'data', '.ipynb_checkpoints', 'Untitled.ipynb', 'dfs', '.log', 'Submit-to-Spark-Cluster.ipynb', 'rstudio', 'common', 'jupyter', 'Tutorial - Spark in Python0.ipynb', 'Tutorial - Spark in Python.ipynb', 'Toronto Neighborhoods.ipynb', 'zeppelin', 'Second Notebook.ipynb']\n"
]
}
],
"source": [
"import os\n",
"\n",
"cwd = os.getcwd() # Get the current working directory (cwd)\n",
"files = os.listdir(cwd) # Get all the files in that directory\n",
"print(\"Files in '%s': %s\" % (cwd, files))\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"table = soup.find('table')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"pd.set_option('display.max_rows', 200)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>PostalCode</th>\n",
" <th>Borough</th>\n",
" <th>Neighborhood</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>M3A</td>\n",
" <td>North York</td>\n",
" <td>Parkwoods\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>M4A</td>\n",
" <td>North York</td>\n",
" <td>Victoria Village\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>M5A</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Harbourfront, Regent Park</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>M5A</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Regent Park\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>M6A</td>\n",
" <td>North York</td>\n",
" <td>Lawrence Heights, Lawrence Manor</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>M6A</td>\n",
" <td>North York</td>\n",
" <td>Lawrence Manor\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>M7A</td>\n",
" <td>Queen's Park</td>\n",
" <td>Queen's Park</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>M9A</td>\n",
" <td>Etobicoke</td>\n",
" <td>Islington Avenue\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Rouge, Malvern</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Malvern\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>M3B</td>\n",
" <td>North York</td>\n",
" <td>Don Mills North\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>M4B</td>\n",
" <td>East York</td>\n",
" <td>Woodbine Gardens, Parkview Hill</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>M4B</td>\n",
" <td>East York</td>\n",
" <td>Parkview Hill\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>M5B</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Ryerson, Garden District</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>M5B</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Garden District\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>M6B</td>\n",
" <td>North York</td>\n",
" <td>Glencairn\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Cloverdale, Islington, Martin Grove, Princess ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Islington\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Martin Grove\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Princess Gardens\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>West Deane Park\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Highland Creek\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Rouge Hill, Port Union, Highland Creek</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Port Union\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <td>M3C</td>\n",
" <td>North York</td>\n",
" <td>Flemingdon Park, Don Mills South</td>\n",
" </tr>\n",
" <tr>\n",
" <th>33</th>\n",
" <td>M3C</td>\n",
" <td>North York</td>\n",
" <td>Don Mills South\\n</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34</th>\n",
" <td>M4C</td>\n",
" <td>East York</td>\n",
" <td>Woodbine Heights\\n</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" PostalCode Borough \\\n",
"0 None None \n",
"3 M3A North York \n",
"4 M4A North York \n",
"5 M5A Downtown Toronto \n",
"6 M5A Downtown Toronto \n",
"7 M6A North York \n",
"8 M6A North York \n",
"9 M7A Queen's Park \n",
"11 M9A Etobicoke \n",
"12 M1B Scarborough \n",
"13 M1B Scarborough \n",
"15 M3B North York \n",
"16 M4B East York \n",
"17 M4B East York \n",
"18 M5B Downtown Toronto \n",
"19 M5B Downtown Toronto \n",
"20 M6B North York \n",
"23 M9B Etobicoke \n",
"24 M9B Etobicoke \n",
"25 M9B Etobicoke \n",
"26 M9B Etobicoke \n",
"27 M9B Etobicoke \n",
"28 M1C Scarborough \n",
"29 M1C Scarborough \n",
"30 M1C Scarborough \n",
"32 M3C North York \n",
"33 M3C North York \n",
"34 M4C East York \n",
"\n",
" Neighborhood \n",
"0 None \n",
"3 Parkwoods\\n \n",
"4 Victoria Village\\n \n",
"5 Harbourfront, Regent Park \n",
"6 Regent Park\\n \n",
"7 Lawrence Heights, Lawrence Manor \n",
"8 Lawrence Manor\\n \n",
"9 Queen's Park \n",
"11 Islington Avenue\\n \n",
"12 Rouge, Malvern \n",
"13 Malvern\\n \n",
"15 Don Mills North\\n \n",
"16 Woodbine Gardens, Parkview Hill \n",
"17 Parkview Hill\\n \n",
"18 Ryerson, Garden District \n",
"19 Garden District\\n \n",
"20 Glencairn\\n \n",
"23 Cloverdale, Islington, Martin Grove, Princess ... \n",
"24 Islington\\n \n",
"25 Martin Grove\\n \n",
"26 Princess Gardens\\n \n",
"27 West Deane Park\\n \n",
"28 Highland Creek\\n \n",
"29 Rouge Hill, Port Union, Highland Creek \n",
"30 Port Union\\n \n",
"32 Flemingdon Park, Don Mills South \n",
"33 Don Mills South\\n \n",
"34 Woodbine Heights\\n "
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = []\n",
"table_rows = table.find_all('tr')\n",
"for tr in table_rows:\n",
" td = tr.find_all('td')\n",
" row = [i.text for i in td]\n",
" l.append(row)\n",
"df3 = pd.DataFrame(l, columns=[\"A\", \"B\", \"C\"])\n",
"df3\n",
"df2 = df3[df3.B != 'Not assigned']\n",
"df2\n",
"df2.columns = ['PostalCode', 'Borough', 'Neighborhood']\n",
"df2\n",
"df2[df2.Borough != 'None']\n",
"df2.at[5, 'Neighborhood'] = \"Harbourfront, Regent Park\"\n",
"df2.at[9, 'Neighborhood'] = \"Queen's Park\"\n",
"df2.at[7, 'Neighborhood'] = \"Lawrence Heights, Lawrence Manor\"\n",
"df2.at[12, 'Neighborhood'] = \"Rouge, Malvern\"\n",
"df2.at[16, 'Neighborhood'] = \"Woodbine Gardens, Parkview Hill\"\n",
"df2.at[18, 'Neighborhood'] = \"Ryerson, Garden District\"\n",
"df2.at[23, 'Neighborhood'] = \"Cloverdale, Islington, Martin Grove, Princess Gardens, West Deane Park\"\n",
"df2.at[29, 'Neighborhood'] = \"Rouge Hill, Port Union, Highland Creek\"\n",
"df2.at[32, 'Neighborhood'] = \"Flemingdon Park, Don Mills South\"\n",
"df2.at[39, 'Neighborhood'] = \"Bloordale Gardens, Eringate, Markland Wood, Old Burnhamthorpe\"\n",
"df2.at[43, 'Neighborhood'] = \"Guildwood, Morningside, West Hill, Old Burnhamthorpe\"\n",
"df2.at[64, 'Neighborhood'] = \"Hillcrest Village, Bathurst Manor, Downsview North, Wilson Heights\"\n",
"df2.at[69, 'Neighborhood'] = \"Hillcrest Village, King, Richmond\"\n",
"df2.at[72, 'Neighborhood'] = \"Dovercourt Village, Dufferin\"\n",
"df2.at[78, 'Neighborhood'] = \"Fairview, Dufferin, Henry Farm, Oriole, Northwood Park, York University\"\n",
"df2.at[84, 'Neighborhood'] = \"Harbourfront East, Toronto Island, Union Station\"\n",
"df2.at[87, 'Neighborhood'] = \"Little Portugal, Trinity\"\n",
"df2.at[93, 'Neighborhood'] = \"Loneview, Kennedy Park\"\n",
"df2.at[95, 'Neighborhood'] = \"Bayview village, CFB Toronto, Downsview East\"\n",
"df2.at[98, 'Neighborhood'] = \"The Danforth West, Riverdale\"\n",
"df2.at[100, 'Neighborhood'] = \"Design Exchange, Toronto Dominion Centre\"\n",
"df2.at[102, 'Neighborhood'] = \"Brockton, Exhibition Place, Parkdale Village\"\n",
"df2.at[108, 'Neighborhood'] = \"Clairlea, Golen Mile, Oakridge\"\n",
"df2.at[111, 'Neighborhood'] = \"Silver Hills, York Mills\"\n",
"df2.at[111, 'Neighborhood'] = \"Silver Hills, York Mills\"\n",
"df2.at[114, 'Neighborhood'] = \"The Beaches West, India Bazaar\"\n",
"df2.at[116, 'Neighborhood'] = \"Commerce court, victoria hotel\"\n",
"df2.at[118, 'Neighborhood'] = \"Downview, North Park, Upwood Park\"\n",
"df2.at[124, 'Neighborhood'] = \"Cliffcrest, Cliffside, SCarborough Village West\"\n",
"df2.at[127, 'Neighborhood'] = \"Newtonobrook, Willodale\"\n",
"df2.at[129, 'Neighborhood'] = \"Downsview central, Studio District\"\n",
"df2.at[131, 'Neighborhood'] = \"Bedford Park, Lawrence Manor East\"\n",
"df2.at[133, 'Neighborhood'] = \"Del Ray, Keelesdale, Mount Dennis, Silverthorn\"\n",
"df2.at[139, 'Neighborhood'] = \"Emery, Humberlea\"\n",
"df2.at[141, 'Neighborhood'] = \"Birch Cliff, Cliffside West\"\n",
"\n",
"\n",
"#df2.drop([0,6,8,13,17,19,24,25,26,27,28,92,30,33,40,41,42,44,45,65,66,67,70,71,\n",
" # 73,79,80,81,82,85,86,88,94,96,97,99,101,103,104,109,110,112,115,117,119,\n",
" #120,125,126,128,130,132,134,135,136,140,142,], axis=0, inplace=True)\n",
"\n",
"#df4 = df2.drop(df2[df2.index > 20].index)\n",
"\n",
"df5 = df2.drop(df2[df2.index > 34].index)\n",
"\n",
"\n",
"#df2 = df2.reset_index()\n",
"\n",
"\n",
"#df4.drop('index', axis=1)\n",
"#Other method to derive table (not used)\n",
"#dfs = pd.read_html('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M')\n",
"#print(dfs)\n",
"#dfs=df;\n",
"df5"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(28, 3)"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shape = df5.shape\n",
"shape "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import geocoder # import geocoder\n",
"from geopy.geocoders import Nominatim\n",
"from geopy.distance import vincenty\n",
"import datetime as DT\n",
"import hmac\n",
"import pandas as pd\n",
"import io\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
"dd"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Postal Code</th>\n",
" <th>Borough</th>\n",
" <th>Neighborhood</th>\n",
" <th>Long</th>\n",
" <th>Lat</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>M3A</td>\n",
" <td>North York</td>\n",
" <td>Parkwoods\\n</td>\n",
" <td>-79.329656</td>\n",
" <td>43.753259</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>M4A</td>\n",
" <td>North York</td>\n",
" <td>Victoria Village\\n</td>\n",
" <td>-79.315572</td>\n",
" <td>43.725882</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>M5A</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Harbourfront, Regent Park</td>\n",
" <td>-79.360636</td>\n",
" <td>43.654260</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>M5A</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Regent Park\\n</td>\n",
" <td>-79.360636</td>\n",
" <td>43.654260</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>M6A</td>\n",
" <td>North York</td>\n",
" <td>Lawrence Heights, Lawrence Manor</td>\n",
" <td>-79.464763</td>\n",
" <td>43.718518</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>M6A</td>\n",
" <td>North York</td>\n",
" <td>Lawrence Manor\\n</td>\n",
" <td>-79.464763</td>\n",
" <td>43.718518</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>M7A</td>\n",
" <td>Queen's Park</td>\n",
" <td>Queen's Park</td>\n",
" <td>-79.389494</td>\n",
" <td>43.662301</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>M9A</td>\n",
" <td>Etobicoke</td>\n",
" <td>Islington Avenue\\n</td>\n",
" <td>-79.532242</td>\n",
" <td>43.667856</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Rouge, Malvern</td>\n",
" <td>-79.194353</td>\n",
" <td>43.806686</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Malvern\\n</td>\n",
" <td>-79.194353</td>\n",
" <td>43.806686</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>M3B</td>\n",
" <td>North York</td>\n",
" <td>Don Mills North\\n</td>\n",
" <td>-79.352188</td>\n",
" <td>43.745906</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>M4B</td>\n",
" <td>East York</td>\n",
" <td>Woodbine Gardens, Parkview Hill</td>\n",
" <td>-79.309937</td>\n",
" <td>43.706397</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>M4B</td>\n",
" <td>East York</td>\n",
" <td>Parkview Hill\\n</td>\n",
" <td>-79.309937</td>\n",
" <td>43.706397</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>M5B</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Ryerson, Garden District</td>\n",
" <td>-79.378937</td>\n",
" <td>43.657162</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>M5B</td>\n",
" <td>Downtown Toronto</td>\n",
" <td>Garden District\\n</td>\n",
" <td>-79.378937</td>\n",
" <td>43.657162</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>M6B</td>\n",
" <td>North York</td>\n",
" <td>Glencairn\\n</td>\n",
" <td>-79.445073</td>\n",
" <td>43.709577</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Cloverdale, Islington, Martin Grove, Princess ...</td>\n",
" <td>-79.554724</td>\n",
" <td>43.650943</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Islington\\n</td>\n",
" <td>-79.554724</td>\n",
" <td>43.650943</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Martin Grove\\n</td>\n",
" <td>-79.554724</td>\n",
" <td>43.650943</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Princess Gardens\\n</td>\n",
" <td>-79.554724</td>\n",
" <td>43.650943</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>West Deane Park\\n</td>\n",
" <td>-79.554724</td>\n",
" <td>43.650943</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Highland Creek\\n</td>\n",
" <td>-79.160497</td>\n",
" <td>43.784535</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Rouge Hill, Port Union, Highland Creek</td>\n",
" <td>-79.160497</td>\n",
" <td>43.784535</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Port Union\\n</td>\n",
" <td>-79.160497</td>\n",
" <td>43.784535</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>M3C</td>\n",
" <td>North York</td>\n",
" <td>Flemingdon Park, Don Mills South</td>\n",
" <td>-79.340923</td>\n",
" <td>43.725900</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>M3C</td>\n",
" <td>North York</td>\n",
" <td>Don Mills South\\n</td>\n",
" <td>-79.340923</td>\n",
" <td>43.725900</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>M4C</td>\n",
" <td>East York</td>\n",
" <td>Woodbine Heights\\n</td>\n",
" <td>-79.318389</td>\n",
" <td>43.695344</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Postal Code Borough \\\n",
"0 M3A North York \n",
"1 M4A North York \n",
"2 M5A Downtown Toronto \n",
"3 M5A Downtown Toronto \n",
"4 M6A North York \n",
"5 M6A North York \n",
"6 M7A Queen's Park \n",
"7 M9A Etobicoke \n",
"8 M1B Scarborough \n",
"9 M1B Scarborough \n",
"10 M3B North York \n",
"11 M4B East York \n",
"12 M4B East York \n",
"13 M5B Downtown Toronto \n",
"14 M5B Downtown Toronto \n",
"15 M6B North York \n",
"16 M9B Etobicoke \n",
"17 M9B Etobicoke \n",
"18 M9B Etobicoke \n",
"19 M9B Etobicoke \n",
"20 M9B Etobicoke \n",
"21 M1C Scarborough \n",
"22 M1C Scarborough \n",
"23 M1C Scarborough \n",
"24 M3C North York \n",
"25 M3C North York \n",
"26 M4C East York \n",
"\n",
" Neighborhood Long Lat \n",
"0 Parkwoods\\n -79.329656 43.753259 \n",
"1 Victoria Village\\n -79.315572 43.725882 \n",
"2 Harbourfront, Regent Park -79.360636 43.654260 \n",
"3 Regent Park\\n -79.360636 43.654260 \n",
"4 Lawrence Heights, Lawrence Manor -79.464763 43.718518 \n",
"5 Lawrence Manor\\n -79.464763 43.718518 \n",
"6 Queen's Park -79.389494 43.662301 \n",
"7 Islington Avenue\\n -79.532242 43.667856 \n",
"8 Rouge, Malvern -79.194353 43.806686 \n",
"9 Malvern\\n -79.194353 43.806686 \n",
"10 Don Mills North\\n -79.352188 43.745906 \n",
"11 Woodbine Gardens, Parkview Hill -79.309937 43.706397 \n",
"12 Parkview Hill\\n -79.309937 43.706397 \n",
"13 Ryerson, Garden District -79.378937 43.657162 \n",
"14 Garden District\\n -79.378937 43.657162 \n",
"15 Glencairn\\n -79.445073 43.709577 \n",
"16 Cloverdale, Islington, Martin Grove, Princess ... -79.554724 43.650943 \n",
"17 Islington\\n -79.554724 43.650943 \n",
"18 Martin Grove\\n -79.554724 43.650943 \n",
"19 Princess Gardens\\n -79.554724 43.650943 \n",
"20 West Deane Park\\n -79.554724 43.650943 \n",
"21 Highland Creek\\n -79.160497 43.784535 \n",
"22 Rouge Hill, Port Union, Highland Creek -79.160497 43.784535 \n",
"23 Port Union\\n -79.160497 43.784535 \n",
"24 Flemingdon Park, Don Mills South -79.340923 43.725900 \n",
"25 Don Mills South\\n -79.340923 43.725900 \n",
"26 Woodbine Heights\\n -79.318389 43.695344 "
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url = \"http://cocl.us/Geospatial_data\"\n",
"s = requests.get(url).content\n",
"ds = pd.read_csv(io.StringIO(s.decode('utf-8')))\n",
"ds.rename(columns={'Latitude':'Lat'}, inplace=True)\n",
"ds.rename(columns={'Longitude':'Long'}, inplace=True)\n",
"df5.rename(columns={'PostalCode':'Postal Code'}, inplace=True)\n",
"df5 = df5.merge(ds[['Postal Code', 'Lat']], on=['Postal Code'])\n",
"df5 = df5.merge(ds[['Postal Code', 'Long']], on=['Postal Code'])\n",
"df5\n"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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