Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Created on Cognitive Class Labs
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np "
]
},
{
"cell_type": "code",
"execution_count": 33,
"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": 34,
"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": 35,
"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": 36,
"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": 13,
"metadata": {},
"outputs": [],
"source": [
"from bs4 import BeautifulSoup\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"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": 15,
"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": 16,
"metadata": {},
"outputs": [],
"source": [
"table = soup.find('table')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"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": 18,
"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",
" </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": [
" Postal Code 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": 18,
"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.rename(columns={'PostalCode':'Postal Code'}, inplace=True)\n",
"df5"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"\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": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(28, 3)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shape = df5.shape\n",
"shape "
]
},
{
"cell_type": "code",
"execution_count": 21,
"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>Latitude</th>\n",
" <th>Longitude</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",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>M3A</td>\n",
" <td>North York</td>\n",
" <td>Parkwoods\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>M9A</td>\n",
" <td>Etobicoke</td>\n",
" <td>Islington Avenue\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Rouge, Malvern</td>\n",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Malvern\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>M6B</td>\n",
" <td>North York</td>\n",
" <td>Glencairn\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Islington\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Martin Grove\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Princess Gardens\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Highland Creek\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>M1C</td>\n",
" <td>Scarborough</td>\n",
" <td>Port Union\\n</td>\n",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</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",
" <td>abc</td>\n",
" <td>abc</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Postal Code 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 Latitude Longitude \n",
"0 None abc abc \n",
"3 Parkwoods\\n abc abc \n",
"4 Victoria Village\\n abc abc \n",
"5 Harbourfront, Regent Park abc abc \n",
"6 Regent Park\\n abc abc \n",
"7 Lawrence Heights, Lawrence Manor abc abc \n",
"8 Lawrence Manor\\n abc abc \n",
"9 Queen's Park abc abc \n",
"11 Islington Avenue\\n abc abc \n",
"12 Rouge, Malvern abc abc \n",
"13 Malvern\\n abc abc \n",
"15 Don Mills North\\n abc abc \n",
"16 Woodbine Gardens, Parkview Hill abc abc \n",
"17 Parkview Hill\\n abc abc \n",
"18 Ryerson, Garden District abc abc \n",
"19 Garden District\\n abc abc \n",
"20 Glencairn\\n abc abc \n",
"23 Cloverdale, Islington, Martin Grove, Princess ... abc abc \n",
"24 Islington\\n abc abc \n",
"25 Martin Grove\\n abc abc \n",
"26 Princess Gardens\\n abc abc \n",
"27 West Deane Park\\n abc abc \n",
"28 Highland Creek\\n abc abc \n",
"29 Rouge Hill, Port Union, Highland Creek abc abc \n",
"30 Port Union\\n abc abc \n",
"32 Flemingdon Park, Don Mills South abc abc \n",
"33 Don Mills South\\n abc abc \n",
"34 Woodbine Heights\\n abc abc "
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df5['Latitude']='abc'\n",
"df5['Longitude']='abc'\n",
"df5"
]
},
{
"cell_type": "code",
"execution_count": 22,
"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>Latitude</th>\n",
" <th>Longitude</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>M1B</td>\n",
" <td>43.806686</td>\n",
" <td>-79.194353</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>M1C</td>\n",
" <td>43.784535</td>\n",
" <td>-79.160497</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>M1E</td>\n",
" <td>43.763573</td>\n",
" <td>-79.188711</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>M1G</td>\n",
" <td>43.770992</td>\n",
" <td>-79.216917</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>M1H</td>\n",
" <td>43.773136</td>\n",
" <td>-79.239476</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Postal Code Latitude Longitude\n",
"0 M1B 43.806686 -79.194353\n",
"1 M1C 43.784535 -79.160497\n",
"2 M1E 43.763573 -79.188711\n",
"3 M1G 43.770992 -79.216917\n",
"4 M1H 43.773136 -79.239476"
]
},
"execution_count": 22,
"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.head()"
]
},
{
"cell_type": "code",
"execution_count": 55,
"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>Latitude</th>\n",
" <th>Longitude</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>43.753259</td>\n",
" <td>-79.329656</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>43.725882</td>\n",
" <td>-79.315572</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>43.654260</td>\n",
" <td>-79.360636</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>43.654260</td>\n",
" <td>-79.360636</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>43.718518</td>\n",
" <td>-79.464763</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>43.718518</td>\n",
" <td>-79.464763</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>43.662301</td>\n",
" <td>-79.389494</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>43.667856</td>\n",
" <td>-79.532242</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Rouge, Malvern</td>\n",
" <td>43.806686</td>\n",
" <td>-79.194353</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>M1B</td>\n",
" <td>Scarborough</td>\n",
" <td>Malvern\\n</td>\n",
" <td>43.806686</td>\n",
" <td>-79.194353</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>43.745906</td>\n",
" <td>-79.352188</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>43.706397</td>\n",
" <td>-79.309937</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>43.706397</td>\n",
" <td>-79.309937</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>43.657162</td>\n",
" <td>-79.378937</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>43.657162</td>\n",
" <td>-79.378937</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>43.709577</td>\n",
" <td>-79.445073</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>43.650943</td>\n",
" <td>-79.554724</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>M9B</td>\n",
" <td>Etobicoke</td>\n",
" <td>Islington\\n</td>\n",
" <td>43.650943</td>\n",
" <td>-79.554724</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>43.650943</td>\n",
" <td>-79.554724</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>43.650943</td>\n",
" <td>-79.554724</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>43.650943</td>\n",
" <td>-79.554724</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>43.784535</td>\n",
" <td>-79.160497</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>43.784535</td>\n",
" <td>-79.160497</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>43.784535</td>\n",
" <td>-79.160497</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>43.725900</td>\n",
" <td>-79.340923</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>43.725900</td>\n",
" <td>-79.340923</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>43.695344</td>\n",
" <td>-79.318389</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 Latitude Longitude \n",
"0 Parkwoods\\n 43.753259 -79.329656 \n",
"1 Victoria Village\\n 43.725882 -79.315572 \n",
"2 Harbourfront, Regent Park 43.654260 -79.360636 \n",
"3 Regent Park\\n 43.654260 -79.360636 \n",
"4 Lawrence Heights, Lawrence Manor 43.718518 -79.464763 \n",
"5 Lawrence Manor\\n 43.718518 -79.464763 \n",
"6 Queen's Park 43.662301 -79.389494 \n",
"7 Islington Avenue\\n 43.667856 -79.532242 \n",
"8 Rouge, Malvern 43.806686 -79.194353 \n",
"9 Malvern\\n 43.806686 -79.194353 \n",
"10 Don Mills North\\n 43.745906 -79.352188 \n",
"11 Woodbine Gardens, Parkview Hill 43.706397 -79.309937 \n",
"12 Parkview Hill\\n 43.706397 -79.309937 \n",
"13 Ryerson, Garden District 43.657162 -79.378937 \n",
"14 Garden District\\n 43.657162 -79.378937 \n",
"15 Glencairn\\n 43.709577 -79.445073 \n",
"16 Cloverdale, Islington, Martin Grove, Princess ... 43.650943 -79.554724 \n",
"17 Islington\\n 43.650943 -79.554724 \n",
"18 Martin Grove\\n 43.650943 -79.554724 \n",
"19 Princess Gardens\\n 43.650943 -79.554724 \n",
"20 West Deane Park\\n 43.650943 -79.554724 \n",
"21 Highland Creek\\n 43.784535 -79.160497 \n",
"22 Rouge Hill, Port Union, Highland Creek 43.784535 -79.160497 \n",
"23 Port Union\\n 43.784535 -79.160497 \n",
"24 Flemingdon Park, Don Mills South 43.725900 -79.340923 \n",
"25 Don Mills South\\n 43.725900 -79.340923 \n",
"26 Woodbine Heights\\n 43.695344 -79.318389 "
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"#ds.rename(columns={'Lat':'Latitude'}, inplace=True)\n",
"#ds.rename(columns={'Long':'Longitude'}, inplace=True)\n",
"#df5.rename(columns={'Lat':'Latitude'}, inplace=True)\n",
"#df5.rename(columns={'Long':'Longitude'}, inplace=True)\n",
"#df5.rename(columns={'PostalCode':'Postal Code'}, inplace=True)\n",
"#df5 = df5.merge(ds[['Postal Code', 'Latitude']], on=['Postal Code'])\n",
"#df5 = df5.merge(ds[['Postal Code', 'Longitude']], on=['Postal Code'])\n",
"#df5.rename(columns={'Latitude_y':'Latitude'}, inplace=True)\n",
"#df5.rename(columns={'Longitude_y':'Longitude'}, inplace=True)\n",
"#df5=df5.drop(columns=['Longitude_x'])\n",
"df5"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # library to handle data in a vectorized manner\n",
"\n",
"import pandas as pd # library for data analsysis\n",
"pd.set_option('display.max_columns', None)\n",
"pd.set_option('display.max_rows', None)\n",
"\n",
"import json # library to handle JSON files\n",
"\n",
"#!conda install -c conda-forge geopy --yes # uncomment this line if you haven't completed the Foursquare API lab\n",
"from geopy.geocoders import Nominatim # convert an address into latitude and longitude values\n",
"\n",
"import requests # library to handle requests\n",
"from pandas.io.json import json_normalize # tranform JSON file into a pandas dataframe\n",
"\n",
"# Matplotlib and associated plotting modules\n",
"import matplotlib.cm as cm\n",
"import matplotlib.colors as colors\n",
"\n",
"# import k-means from clustering stage\n",
"from sklearn.cluster import KMeans\n",
"\n",
"#!conda install -c conda-forge folium=0.5.0 --yes # uncomment this line if you haven't completed the Foursquare API lab\n",
"import folium # map rendering library\n"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your credentails:\n",
"CLIENT_ID: PJS5VPSQJK5GSZVFE2QVMOMLPYLINYY1XLXGOU31EELHT1MJ\n",
"CLIENT_SECRET:GPZVYLDEHPZPGQ0RPXZHNSQU2T3TDWNY2DGDP5H1H4EM5GRR\n"
]
}
],
"source": [
"CLIENT_ID = 'PJS5VPSQJK5GSZVFE2QVMOMLPYLINYY1XLXGOU31EELHT1MJ' # your Foursquare ID\n",
"CLIENT_SECRET = 'GPZVYLDEHPZPGQ0RPXZHNSQU2T3TDWNY2DGDP5H1H4EM5GRR' # your Foursquare Secret\n",
"VERSION = '20180605' # Foursquare API version\n",
"\n",
"print('Your credentails:')\n",
"print('CLIENT_ID: ' + CLIENT_ID)\n",
"print('CLIENT_SECRET:' + CLIENT_SECRET)\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"#results = requests.get(url).json()\n",
"#results\n",
"df5.loc[0, 'Borough']"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Latitude and longitude values of North York are 43.7532586, -79.3296565.\n"
]
}
],
"source": [
"neighborhood_latitude = df5.loc[0, 'Latitude'] # neighborhood latitude value\n",
"neighborhood_longitude = df5.loc[0, 'Longitude'] # neighborhood longitude value\n",
"\n",
"neighborhood_name = df5.loc[0, 'Borough'] # neighborhood name\n",
"\n",
"print('Latitude and longitude values of {} are {}, {}.'.format(neighborhood_name, \n",
" neighborhood_latitude, \n",
" neighborhood_longitude))\n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'https://api.foursquare.com/v2/venues/explore?&client_id=PJS5VPSQJK5GSZVFE2QVMOMLPYLINYY1XLXGOU31EELHT1MJ&client_secret=GPZVYLDEHPZPGQ0RPXZHNSQU2T3TDWNY2DGDP5H1H4EM5GRR&v=20180605&ll=43.7532586,-79.3296565&radius=500&limit=100'"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"LIMIT = 100 # limit of number of venues returned by Foursquare API\n",
"radius = 500 # define radius\n",
"# create URL\n",
"url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(\n",
" CLIENT_ID, \n",
" CLIENT_SECRET, \n",
" VERSION, \n",
" neighborhood_latitude, \n",
" neighborhood_longitude, \n",
" radius, \n",
" LIMIT)\n",
"url # display URL\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'meta': {'code': 200, 'requestId': '5d27cf0ba87921002c53f5a4'},\n",
" 'response': {'warning': {'text': \"There aren't a lot of results near you. Try something more general, reset your filters, or expand the search area.\"},\n",
" 'headerLocation': 'Parkwoods - Donalda',\n",
" 'headerFullLocation': 'Parkwoods - Donalda, Toronto',\n",
" 'headerLocationGranularity': 'neighborhood',\n",
" 'totalResults': 3,\n",
" 'suggestedBounds': {'ne': {'lat': 43.757758604500005,\n",
" 'lng': -79.32343823984928},\n",
" 'sw': {'lat': 43.7487585955, 'lng': -79.33587476015072}},\n",
" 'groups': [{'type': 'Recommended Places',\n",
" 'name': 'recommended',\n",
" 'items': [{'reasons': {'count': 0,\n",
" 'items': [{'summary': 'This spot is popular',\n",
" 'type': 'general',\n",
" 'reasonName': 'globalInteractionReason'}]},\n",
" 'venue': {'id': '4e8d9dcdd5fbbbb6b3003c7b',\n",
" 'name': 'Brookbanks Park',\n",
" 'location': {'address': 'Toronto',\n",
" 'lat': 43.751976046055574,\n",
" 'lng': -79.33214044722958,\n",
" 'labeledLatLngs': [{'label': 'display',\n",
" 'lat': 43.751976046055574,\n",
" 'lng': -79.33214044722958}],\n",
" 'distance': 245,\n",
" 'cc': 'CA',\n",
" 'city': 'Toronto',\n",
" 'state': 'ON',\n",
" 'country': 'Canada',\n",
" 'formattedAddress': ['Toronto', 'Toronto ON', 'Canada']},\n",
" 'categories': [{'id': '4bf58dd8d48988d163941735',\n",
" 'name': 'Park',\n",
" 'pluralName': 'Parks',\n",
" 'shortName': 'Park',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/parks_outdoors/park_',\n",
" 'suffix': '.png'},\n",
" 'primary': True}],\n",
" 'photos': {'count': 0, 'groups': []}},\n",
" 'referralId': 'e-0-4e8d9dcdd5fbbbb6b3003c7b-0'},\n",
" {'reasons': {'count': 0,\n",
" 'items': [{'summary': 'This spot is popular',\n",
" 'type': 'general',\n",
" 'reasonName': 'globalInteractionReason'}]},\n",
" 'venue': {'id': '4e6696b6d16433b9ffff47c3',\n",
" 'name': 'KFC',\n",
" 'location': {'lat': 43.75438666345904,\n",
" 'lng': -79.3330206627504,\n",
" 'labeledLatLngs': [{'label': 'display',\n",
" 'lat': 43.75438666345904,\n",
" 'lng': -79.3330206627504}],\n",
" 'distance': 298,\n",
" 'cc': 'CA',\n",
" 'country': 'Canada',\n",
" 'formattedAddress': ['Canada']},\n",
" 'categories': [{'id': '4bf58dd8d48988d16e941735',\n",
" 'name': 'Fast Food Restaurant',\n",
" 'pluralName': 'Fast Food Restaurants',\n",
" 'shortName': 'Fast Food',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/fastfood_',\n",
" 'suffix': '.png'},\n",
" 'primary': True}],\n",
" 'photos': {'count': 0, 'groups': []}},\n",
" 'referralId': 'e-0-4e6696b6d16433b9ffff47c3-1'},\n",
" {'reasons': {'count': 0,\n",
" 'items': [{'summary': 'This spot is popular',\n",
" 'type': 'general',\n",
" 'reasonName': 'globalInteractionReason'}]},\n",
" 'venue': {'id': '4cb11e2075ebb60cd1c4caad',\n",
" 'name': 'Variety Store',\n",
" 'location': {'address': '29 Valley Woods Road',\n",
" 'lat': 43.75197441585782,\n",
" 'lng': -79.33311418516017,\n",
" 'labeledLatLngs': [{'label': 'display',\n",
" 'lat': 43.75197441585782,\n",
" 'lng': -79.33311418516017}],\n",
" 'distance': 312,\n",
" 'cc': 'CA',\n",
" 'city': 'Toronto',\n",
" 'state': 'ON',\n",
" 'country': 'Canada',\n",
" 'formattedAddress': ['29 Valley Woods Road', 'Toronto ON', 'Canada']},\n",
" 'categories': [{'id': '4bf58dd8d48988d1f9941735',\n",
" 'name': 'Food & Drink Shop',\n",
" 'pluralName': 'Food & Drink Shops',\n",
" 'shortName': 'Food & Drink',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/shops/foodanddrink_',\n",
" 'suffix': '.png'},\n",
" 'primary': True}],\n",
" 'photos': {'count': 0, 'groups': []}},\n",
" 'referralId': 'e-0-4cb11e2075ebb60cd1c4caad-2'}]}]}}"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results = requests.get(url).json()\n",
"results\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def get_category_type(row):\n",
" try:\n",
" categories_list = row['categories']\n",
" except:\n",
" categories_list = row['venue.categories']\n",
" \n",
" if len(categories_list) == 0:\n",
" return None\n",
" else:\n",
" return categories_list[0]['name']"
]
},
{
"cell_type": "code",
"execution_count": 62,
"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>name</th>\n",
" <th>categories</th>\n",
" <th>lat</th>\n",
" <th>lng</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Brookbanks Park</td>\n",
" <td>Park</td>\n",
" <td>43.751976</td>\n",
" <td>-79.332140</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>KFC</td>\n",
" <td>Fast Food Restaurant</td>\n",
" <td>43.754387</td>\n",
" <td>-79.333021</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Variety Store</td>\n",
" <td>Food &amp; Drink Shop</td>\n",
" <td>43.751974</td>\n",
" <td>-79.333114</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name categories lat lng\n",
"0 Brookbanks Park Park 43.751976 -79.332140\n",
"1 KFC Fast Food Restaurant 43.754387 -79.333021\n",
"2 Variety Store Food & Drink Shop 43.751974 -79.333114"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"venues = results['response']['groups'][0]['items']\n",
" \n",
"nearby_venues = json_normalize(venues) # flatten JSON\n",
"\n",
"# filter columns\n",
"filtered_columns = ['venue.name', 'venue.categories', 'venue.location.lat', 'venue.location.lng']\n",
"nearby_venues =nearby_venues.loc[:, filtered_columns]\n",
"\n",
"# filter the category for each row\n",
"nearby_venues['venue.categories'] = nearby_venues.apply(get_category_type, axis=1)\n",
"\n",
"# clean columns\n",
"nearby_venues.columns = [col.split(\".\")[-1] for col in nearby_venues.columns]\n",
"\n",
"nearby_venues.head()"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 venues were returned by Foursquare.\n"
]
}
],
"source": [
"print('{} venues were returned by Foursquare.'.format(nearby_venues.shape[0]))"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"def getNearbyVenues(names, latitudes, longitudes, radius=500):\n",
" \n",
" venues_list=[]\n",
" for name, lat, lng in zip(names, latitudes, longitudes):\n",
" print(name)\n",
" \n",
" # create the API request URL\n",
" url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(\n",
" CLIENT_ID, \n",
" CLIENT_SECRET, \n",
" VERSION, \n",
" lat, \n",
" lng, \n",
" radius, \n",
" LIMIT)\n",
" \n",
" # make the GET request\n",
" results = requests.get(url).json()[\"response\"]['groups'][0]['items']\n",
" \n",
" # return only relevant information for each nearby venue\n",
" venues_list.append([(\n",
" name, \n",
" lat, \n",
" lng, \n",
" v['venue']['name'], \n",
" v['venue']['location']['lat'], \n",
" v['venue']['location']['lng'], \n",
" v['venue']['categories'][0]['name']) for v in results])\n",
"\n",
" nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])\n",
" nearby_venues.columns = ['Neighborhood', \n",
" 'Neighborhood Latitude', \n",
" 'Neighborhood Longitude', \n",
" 'Venue', \n",
" 'Venue Latitude', \n",
" 'Venue Longitude', \n",
" 'Venue Category']\n",
" \n",
" return(nearby_venues)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"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>name</th>\n",
" <th>categories</th>\n",
" <th>lat</th>\n",
" <th>lng</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Brookbanks Park</td>\n",
" <td>Park</td>\n",
" <td>43.751976</td>\n",
" <td>-79.332140</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>KFC</td>\n",
" <td>Fast Food Restaurant</td>\n",
" <td>43.754387</td>\n",
" <td>-79.333021</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Variety Store</td>\n",
" <td>Food &amp; Drink Shop</td>\n",
" <td>43.751974</td>\n",
" <td>-79.333114</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name categories lat lng\n",
"0 Brookbanks Park Park 43.751976 -79.332140\n",
"1 KFC Fast Food Restaurant 43.754387 -79.333021\n",
"2 Variety Store Food & Drink Shop 43.751974 -79.333114"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"venues = results['response']['groups'][0]['items']\n",
" \n",
"nearby_venues = json_normalize(venues) # flatten JSON\n",
"\n",
"# filter columns\n",
"filtered_columns = ['venue.name', 'venue.categories', 'venue.location.lat', 'venue.location.lng']\n",
"nearby_venues =nearby_venues.loc[:, filtered_columns]\n",
"\n",
"# filter the category for each row\n",
"nearby_venues['venue.categories'] = nearby_venues.apply(get_category_type, axis=1)\n",
"\n",
"# clean columns\n",
"nearby_venues.columns = [col.split(\".\")[-1] for col in nearby_venues.columns]\n",
"\n",
"nearby_venues.head()"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Parkwoods\n",
"\n",
"Victoria Village\n",
"\n",
"Harbourfront, Regent Park\n",
"Regent Park\n",
"\n",
"Lawrence Heights, Lawrence Manor\n",
"Lawrence Manor\n",
"\n",
"Queen's Park\n",
"Islington Avenue\n",
"\n",
"Rouge, Malvern\n",
"Malvern\n",
"\n",
"Don Mills North\n",
"\n",
"Woodbine Gardens, Parkview Hill\n",
"Parkview Hill\n",
"\n",
"Ryerson, Garden District\n",
"Garden District\n",
"\n",
"Glencairn\n",
"\n",
"Cloverdale, Islington, Martin Grove, Princess Gardens, West Deane Park\n",
"Islington\n",
"\n",
"Martin Grove\n",
"\n",
"Princess Gardens\n",
"\n",
"West Deane Park\n",
"\n",
"Highland Creek\n",
"\n",
"Rouge Hill, Port Union, Highland Creek\n",
"Port Union\n",
"\n",
"Flemingdon Park, Don Mills South\n",
"Don Mills South\n",
"\n",
"Woodbine Heights\n",
"\n"
]
}
],
"source": [
"toronto_venues = getNearbyVenues(names=df5['Neighborhood'],\n",
" latitudes=df5['Latitude'],\n",
" longitudes=df5['Longitude'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(469, 7)\n"
]
},
{
"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>Neighborhood</th>\n",
" <th>Neighborhood Latitude</th>\n",
" <th>Neighborhood Longitude</th>\n",
" <th>Venue</th>\n",
" <th>Venue Latitude</th>\n",
" <th>Venue Longitude</th>\n",
" <th>Venue Category</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Parkwoods\\n</td>\n",
" <td>43.753259</td>\n",
" <td>-79.329656</td>\n",
" <td>Brookbanks Park</td>\n",
" <td>43.751976</td>\n",
" <td>-79.332140</td>\n",
" <td>Park</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Parkwoods\\n</td>\n",
" <td>43.753259</td>\n",
" <td>-79.329656</td>\n",
" <td>KFC</td>\n",
" <td>43.754387</td>\n",
" <td>-79.333021</td>\n",
" <td>Fast Food Restaurant</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Parkwoods\\n</td>\n",
" <td>43.753259</td>\n",
" <td>-79.329656</td>\n",
" <td>Variety Store</td>\n",
" <td>43.751974</td>\n",
" <td>-79.333114</td>\n",
" <td>Food &amp; Drink Shop</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Victoria Village\\n</td>\n",
" <td>43.725882</td>\n",
" <td>-79.315572</td>\n",
" <td>Victoria Village Arena</td>\n",
" <td>43.723481</td>\n",
" <td>-79.315635</td>\n",
" <td>Hockey Arena</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Victoria Village\\n</td>\n",
" <td>43.725882</td>\n",
" <td>-79.315572</td>\n",
" <td>Tim Hortons</td>\n",
" <td>43.725517</td>\n",
" <td>-79.313103</td>\n",
" <td>Coffee Shop</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Neighborhood Neighborhood Latitude Neighborhood Longitude \\\n",
"0 Parkwoods\\n 43.753259 -79.329656 \n",
"1 Parkwoods\\n 43.753259 -79.329656 \n",
"2 Parkwoods\\n 43.753259 -79.329656 \n",
"3 Victoria Village\\n 43.725882 -79.315572 \n",
"4 Victoria Village\\n 43.725882 -79.315572 \n",
"\n",
" Venue Venue Latitude Venue Longitude \\\n",
"0 Brookbanks Park 43.751976 -79.332140 \n",
"1 KFC 43.754387 -79.333021 \n",
"2 Variety Store 43.751974 -79.333114 \n",
"3 Victoria Village Arena 43.723481 -79.315635 \n",
"4 Tim Hortons 43.725517 -79.313103 \n",
"\n",
" Venue Category \n",
"0 Park \n",
"1 Fast Food Restaurant \n",
"2 Food & Drink Shop \n",
"3 Hockey Arena \n",
"4 Coffee Shop "
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(toronto_venues.shape)\n",
"toronto_venues.head()"
]
},
{
"cell_type": "code",
"execution_count": 68,
"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>Neighborhood Latitude</th>\n",
" <th>Neighborhood Longitude</th>\n",
" <th>Venue</th>\n",
" <th>Venue Latitude</th>\n",
" <th>Venue Longitude</th>\n",
" <th>Venue Category</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Neighborhood</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Cloverdale, Islington, Martin Grove, Princess Gardens, West Deane Park</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Don Mills North\\n</th>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Don Mills South\\n</th>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Flemingdon Park, Don Mills South</th>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Garden District\\n</th>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Glencairn\\n</th>\n",
" <td>6</td>\n",
" <td>6</td>\n",
" <td>6</td>\n",
" <td>6</td>\n",
" <td>6</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Harbourfront, Regent Park</th>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Highland Creek\\n</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Islington\\n</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Lawrence Heights, Lawrence Manor</th>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Lawrence Manor\\n</th>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Malvern\\n</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Martin Grove\\n</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Parkview Hill\\n</th>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Parkwoods\\n</th>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Port Union\\n</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Princess Gardens\\n</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Queen's Park</th>\n",
" <td>39</td>\n",
" <td>39</td>\n",
" <td>39</td>\n",
" <td>39</td>\n",
" <td>39</td>\n",
" <td>39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Regent Park\\n</th>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" <td>50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Rouge Hill, Port Union, Highland Creek</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Rouge, Malvern</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Ryerson, Garden District</th>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" <td>100</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Victoria Village\\n</th>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>West Deane Park\\n</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Woodbine Gardens, Parkview Hill</th>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" <td>13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Woodbine Heights\\n</th>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Neighborhood Latitude \\\n",
"Neighborhood \n",
"Cloverdale, Islington, Martin Grove, Princess G... 1 \n",
"Don Mills North\\n 5 \n",
"Don Mills South\\n 22 \n",
"Flemingdon Park, Don Mills South 22 \n",
"Garden District\\n 100 \n",
"Glencairn\\n 6 \n",
"Harbourfront, Regent Park 50 \n",
"Highland Creek\\n 1 \n",
"Islington\\n 1 \n",
"Lawrence Heights, Lawrence Manor 11 \n",
"Lawrence Manor\\n 11 \n",
"Malvern\\n 2 \n",
"Martin Grove\\n 1 \n",
"Parkview Hill\\n 13 \n",
"Parkwoods\\n 3 \n",
"Port Union\\n 1 \n",
"Princess Gardens\\n 1 \n",
"Queen's Park 39 \n",
"Regent Park\\n 50 \n",
"Rouge Hill, Port Union, Highland Creek 1 \n",
"Rouge, Malvern 2 \n",
"Ryerson, Garden District 100 \n",
"Victoria Village\\n 4 \n",
"West Deane Park\\n 1 \n",
"Woodbine Gardens, Parkview Hill 13 \n",
"Woodbine Heights\\n 8 \n",
"\n",
" Neighborhood Longitude \\\n",
"Neighborhood \n",
"Cloverdale, Islington, Martin Grove, Princess G... 1 \n",
"Don Mills North\\n 5 \n",
"Don Mills South\\n 22 \n",
"Flemingdon Park, Don Mills South 22 \n",
"Garden District\\n 100 \n",
"Glencairn\\n 6 \n",
"Harbourfront, Regent Park 50 \n",
"Highland Creek\\n 1 \n",
"Islington\\n 1 \n",
"Lawrence Heights, Lawrence Manor 11 \n",
"Lawrence Manor\\n 11 \n",
"Malvern\\n 2 \n",
"Martin Grove\\n 1 \n",
"Parkview Hill\\n 13 \n",
"Parkwoods\\n 3 \n",
"Port Union\\n 1 \n",
"Princess Gardens\\n 1 \n",
"Queen's Park 39 \n",
"Regent Park\\n 50 \n",
"Rouge Hill, Port Union, Highland Creek 1 \n",
"Rouge, Malvern 2 \n",
"Ryerson, Garden District 100 \n",
"Victoria Village\\n 4 \n",
"West Deane Park\\n 1 \n",
"Woodbine Gardens, Parkview Hill 13 \n",
"Woodbine Heights\\n 8 \n",
"\n",
" Venue Venue Latitude \\\n",
"Neighborhood \n",
"Cloverdale, Islington, Martin Grove, Princess G... 1 1 \n",
"Don Mills North\\n 5 5 \n",
"Don Mills South\\n 22 22 \n",
"Flemingdon Park, Don Mills South 22 22 \n",
"Garden District\\n 100 100 \n",
"Glencairn\\n 6 6 \n",
"Harbourfront, Regent Park 50 50 \n",
"Highland Creek\\n 1 1 \n",
"Islington\\n 1 1 \n",
"Lawrence Heights, Lawrence Manor 11 11 \n",
"Lawrence Manor\\n 11 11 \n",
"Malvern\\n 2 2 \n",
"Martin Grove\\n 1 1 \n",
"Parkview Hill\\n 13 13 \n",
"Parkwoods\\n 3 3 \n",
"Port Union\\n 1 1 \n",
"Princess Gardens\\n 1 1 \n",
"Queen's Park 39 39 \n",
"Regent Park\\n 50 50 \n",
"Rouge Hill, Port Union, Highland Creek 1 1 \n",
"Rouge, Malvern 2 2 \n",
"Ryerson, Garden District 100 100 \n",
"Victoria Village\\n 4 4 \n",
"West Deane Park\\n 1 1 \n",
"Woodbine Gardens, Parkview Hill 13 13 \n",
"Woodbine Heights\\n 8 8 \n",
"\n",
" Venue Longitude \\\n",
"Neighborhood \n",
"Cloverdale, Islington, Martin Grove, Princess G... 1 \n",
"Don Mills North\\n 5 \n",
"Don Mills South\\n 22 \n",
"Flemingdon Park, Don Mills South 22 \n",
"Garden District\\n 100 \n",
"Glencairn\\n 6 \n",
"Harbourfront, Regent Park 50 \n",
"Highland Creek\\n 1 \n",
"Islington\\n 1 \n",
"Lawrence Heights, Lawrence Manor 11 \n",
"Lawrence Manor\\n 11 \n",
"Malvern\\n 2 \n",
"Martin Grove\\n 1 \n",
"Parkview Hill\\n 13 \n",
"Parkwoods\\n 3 \n",
"Port Union\\n 1 \n",
"Princess Gardens\\n 1 \n",
"Queen's Park 39 \n",
"Regent Park\\n 50 \n",
"Rouge Hill, Port Union, Highland Creek 1 \n",
"Rouge, Malvern 2 \n",
"Ryerson, Garden District 100 \n",
"Victoria Village\\n 4 \n",
"West Deane Park\\n 1 \n",
"Woodbine Gardens, Parkview Hill 13 \n",
"Woodbine Heights\\n 8 \n",
"\n",
" Venue Category \n",
"Neighborhood \n",
"Cloverdale, Islington, Martin Grove, Princess G... 1 \n",
"Don Mills North\\n 5 \n",
"Don Mills South\\n 22 \n",
"Flemingdon Park, Don Mills South 22 \n",
"Garden District\\n 100 \n",
"Glencairn\\n 6 \n",
"Harbourfront, Regent Park 50 \n",
"Highland Creek\\n 1 \n",
"Islington\\n 1 \n",
"Lawrence Heights, Lawrence Manor 11 \n",
"Lawrence Manor\\n 11 \n",
"Malvern\\n 2 \n",
"Martin Grove\\n 1 \n",
"Parkview Hill\\n 13 \n",
"Parkwoods\\n 3 \n",
"Port Union\\n 1 \n",
"Princess Gardens\\n 1 \n",
"Queen's Park 39 \n",
"Regent Park\\n 50 \n",
"Rouge Hill, Port Union, Highland Creek 1 \n",
"Rouge, Malvern 2 \n",
"Ryerson, Garden District 100 \n",
"Victoria Village\\n 4 \n",
"West Deane Park\\n 1 \n",
"Woodbine Gardens, Parkview Hill 13 \n",
"Woodbine Heights\\n 8 "
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"toronto_venues.groupby('Neighborhood').count()"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are 113 uniques categories.\n"
]
}
],
"source": [
"print('There are {} uniques categories.'.format(len(toronto_venues['Venue Category'].unique())))"
]
},
{
"cell_type": "code",
"execution_count": 70,
"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>Neighborhood</th>\n",
" <th>Accessories Store</th>\n",
" <th>American Restaurant</th>\n",
" <th>Antique Shop</th>\n",
" <th>Art Gallery</th>\n",
" <th>Arts &amp; Crafts Store</th>\n",
" <th>Asian Restaurant</th>\n",
" <th>Athletics &amp; Sports</th>\n",
" <th>Bakery</th>\n",
" <th>Bank</th>\n",
" <th>Bar</th>\n",
" <th>Basketball Court</th>\n",
" <th>Beer Bar</th>\n",
"