Skip to content

Instantly share code, notes, and snippets.

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 tholsar/0698a53fe107b16788ca2cfa0d908935 to your computer and use it in GitHub Desktop.
Save tholsar/0698a53fe107b16788ca2cfa0d908935 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a href=\"https://cognitiveclass.ai\"><img src = \"https://ibm.box.com/shared/static/9gegpsmnsoo25ikkbl4qzlvlyjbgxs5x.png\" width = 400> </a>\n",
"\n",
"<h1 align=center><font size = 5>Learning FourSquare API with Python</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Introduction\n",
"\n",
"In this lab, you will learn in details how to make calls to the Foursquare API for different purposes. You will learn how to construct a URL to send a request to the API to search for a specific type of venues, to explore a particular venue, to explore a Foursquare user, to explore a geographical location, and to get trending venues around a location. Also, you will learn how to use the visualization library, Folium, to visualize the results."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Table of Contents\n",
"\n",
"1. <a href=\"#item1\">Foursquare API Search Function</a>\n",
"2. <a href=\"#item2\">Explore a Given Venue</a> \n",
"3. <a href=\"#item3\">Explore a User</a> \n",
"4. <a href=\"#item4\">Foursquare API Explore Function</a> \n",
"5. <a href=\"#item5\">Get Trending Venues</a> "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Import necessary Libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting package metadata (current_repodata.json): done\n",
"Solving environment: done\n",
"\n",
"## Package Plan ##\n",
"\n",
" environment location: /home/jupyterlab/conda/envs/python\n",
"\n",
" added / updated specs:\n",
" - geopy\n",
"\n",
"\n",
"The following packages will be downloaded:\n",
"\n",
" package | build\n",
" ---------------------------|-----------------\n",
" geographiclib-1.50 | py_0 34 KB conda-forge\n",
" geopy-2.0.0 | pyh9f0ad1d_0 63 KB conda-forge\n",
" openssl-1.1.1g | h516909a_1 2.1 MB conda-forge\n",
" ------------------------------------------------------------\n",
" Total: 2.2 MB\n",
"\n",
"The following NEW packages will be INSTALLED:\n",
"\n",
" geographiclib conda-forge/noarch::geographiclib-1.50-py_0\n",
" geopy conda-forge/noarch::geopy-2.0.0-pyh9f0ad1d_0\n",
"\n",
"The following packages will be UPDATED:\n",
"\n",
" openssl 1.1.1g-h516909a_0 --> 1.1.1g-h516909a_1\n",
"\n",
"\n",
"\n",
"Downloading and Extracting Packages\n",
"openssl-1.1.1g | 2.1 MB | ##################################### | 100% \n",
"geopy-2.0.0 | 63 KB | ##################################### | 100% \n",
"geographiclib-1.50 | 34 KB | ##################################### | 100% \n",
"Preparing transaction: done\n",
"Verifying transaction: done\n",
"Executing transaction: done\n",
"Collecting package metadata (current_repodata.json): done\n",
"Solving environment: failed with initial frozen solve. Retrying with flexible solve.\n",
"Collecting package metadata (repodata.json): done\n",
"Solving environment: done\n",
"\n",
"## Package Plan ##\n",
"\n",
" environment location: /home/jupyterlab/conda/envs/python\n",
"\n",
" added / updated specs:\n",
" - folium=0.5.0\n",
"\n",
"\n",
"The following packages will be downloaded:\n",
"\n",
" package | build\n",
" ---------------------------|-----------------\n",
" altair-4.1.0 | py_1 614 KB conda-forge\n",
" branca-0.4.1 | py_0 26 KB conda-forge\n",
" brotlipy-0.7.0 |py36h8c4c3a4_1000 346 KB conda-forge\n",
" chardet-3.0.4 |py36h9f0ad1d_1006 188 KB conda-forge\n",
" cryptography-3.0 | py36h45558ae_0 640 KB conda-forge\n",
" folium-0.5.0 | py_0 45 KB conda-forge\n",
" pandas-1.1.0 | py36h831f99a_0 10.5 MB conda-forge\n",
" pysocks-1.7.1 | py36h9f0ad1d_1 27 KB conda-forge\n",
" toolz-0.10.0 | py_0 46 KB conda-forge\n",
" vincent-0.4.4 | py_1 28 KB conda-forge\n",
" ------------------------------------------------------------\n",
" Total: 12.4 MB\n",
"\n",
"The following NEW packages will be INSTALLED:\n",
"\n",
" altair conda-forge/noarch::altair-4.1.0-py_1\n",
" attrs conda-forge/noarch::attrs-19.3.0-py_0\n",
" branca conda-forge/noarch::branca-0.4.1-py_0\n",
" brotlipy conda-forge/linux-64::brotlipy-0.7.0-py36h8c4c3a4_1000\n",
" chardet conda-forge/linux-64::chardet-3.0.4-py36h9f0ad1d_1006\n",
" cryptography conda-forge/linux-64::cryptography-3.0-py36h45558ae_0\n",
" entrypoints conda-forge/linux-64::entrypoints-0.3-py36h9f0ad1d_1001\n",
" folium conda-forge/noarch::folium-0.5.0-py_0\n",
" idna conda-forge/noarch::idna-2.10-pyh9f0ad1d_0\n",
" importlib_metadata conda-forge/noarch::importlib_metadata-1.7.0-0\n",
" jinja2 conda-forge/noarch::jinja2-2.11.2-pyh9f0ad1d_0\n",
" jsonschema conda-forge/linux-64::jsonschema-3.2.0-py36h9f0ad1d_1\n",
" markupsafe conda-forge/linux-64::markupsafe-1.1.1-py36h8c4c3a4_1\n",
" pandas conda-forge/linux-64::pandas-1.1.0-py36h831f99a_0\n",
" pyopenssl conda-forge/noarch::pyopenssl-19.1.0-py_1\n",
" pyrsistent conda-forge/linux-64::pyrsistent-0.16.0-py36h8c4c3a4_0\n",
" pysocks conda-forge/linux-64::pysocks-1.7.1-py36h9f0ad1d_1\n",
" pytz conda-forge/noarch::pytz-2020.1-pyh9f0ad1d_0\n",
" requests conda-forge/noarch::requests-2.24.0-pyh9f0ad1d_0\n",
" toolz conda-forge/noarch::toolz-0.10.0-py_0\n",
" urllib3 conda-forge/noarch::urllib3-1.25.10-py_0\n",
" vincent conda-forge/noarch::vincent-0.4.4-py_1\n",
"\n",
"\n",
"\n",
"Downloading and Extracting Packages\n",
"pandas-1.1.0 | 10.5 MB | ##################################### | 100% \n",
"pysocks-1.7.1 | 27 KB | ##################################### | 100% \n",
"toolz-0.10.0 | 46 KB | ##################################### | 100% \n",
"chardet-3.0.4 | 188 KB | ##################################### | 100% \n",
"folium-0.5.0 | 45 KB | ##################################### | 100% \n",
"cryptography-3.0 | 640 KB | ##################################### | 100% \n",
"branca-0.4.1 | 26 KB | ##################################### | 100% \n",
"brotlipy-0.7.0 | 346 KB | ##################################### | 100% \n",
"altair-4.1.0 | 614 KB | ##################################### | 100% \n",
"vincent-0.4.4 | 28 KB | ##################################### | 100% \n",
"Preparing transaction: done\n",
"Verifying transaction: done\n",
"Executing transaction: done\n",
"Folium installed\n",
"Libraries imported.\n"
]
}
],
"source": [
"import requests # library to handle requests\n",
"import pandas as pd # library for data analsysis\n",
"import numpy as np # library to handle data in a vectorized manner\n",
"import random # library for random number generation\n",
"\n",
"!conda install -c conda-forge geopy --yes \n",
"from geopy.geocoders import Nominatim # module to convert an address into latitude and longitude values\n",
"\n",
"# libraries for displaying images\n",
"from IPython.display import Image \n",
"from IPython.core.display import HTML \n",
"\n",
"\n",
"\n",
"!conda install -c conda-forge folium=0.5.0 --yes\n",
"import folium # plotting library\n",
"\n",
"print('Folium installed')\n",
"print('Libraries imported.')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
" \n",
"# tranforming json file into a pandas dataframe library\n",
"#from pandas.io.json import json_normalize\n",
"from pandas import json_normalize"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Define Foursquare Credentials and Version"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"##### Make sure that you have created a Foursquare developer account and have your credentials handy"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your credentails:\n",
"CLIENT_ID: IZYZU23GR0ISP3CJ50HE5HTKLOURYKPY43NFKFO025ZU4I0D\n",
"CLIENT_SECRET:QKQ5T44KHSFQRAX32GKZFP0PWH4M53YXXC3QWA4P15ZHOQUV\n"
]
}
],
"source": [
"CLIENT_ID = 'IZYZU23GR0ISP3CJ50HE5HTKLOURYKPY43NFKFO025ZU4I0D' # your Foursquare ID\n",
"CLIENT_SECRET = 'QKQ5T44KHSFQRAX32GKZFP0PWH4M53YXXC3QWA4P15ZHOQUV' # your Foursquare Secret\n",
"VERSION = '20200101'\n",
"LIMIT = 30\n",
"print('Your credentails:')\n",
"print('CLIENT_ID: ' + CLIENT_ID)\n",
"print('CLIENT_SECRET:' + CLIENT_SECRET)"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Let's again assume that you are staying at the Conrad hotel. So let's start by converting the Contrad Hotel's address to its latitude and longitude coordinates."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to define an instance of the geocoder, we need to define a user_agent. We will name our agent <em>foursquare_agent</em>, as shown below."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"40.7149555 -74.0153365\n"
]
}
],
"source": [
"address = '102 North End Ave, New York, NY'\n",
"#address = '206 Carnegie Center Dr, Princeton, NJ'\n",
"\n",
"geolocator = Nominatim(user_agent=\"foursquare_agent\")\n",
"location = geolocator.geocode(address)\n",
"latitude = location.latitude\n",
"longitude = location.longitude\n",
"print(latitude, longitude)"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item1\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## 1. Search for a specific venue category\n",
"> `https://api.foursquare.com/v2/venues/`**search**`?client_id=`**CLIENT_ID**`&client_secret=`**CLIENT_SECRET**`&ll=`**LATITUDE**`,`**LONGITUDE**`&v=`**VERSION**`&query=`**QUERY**`&radius=`**RADIUS**`&limit=`**LIMIT**"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Now, let's assume that it is lunch time, and you are craving Italian food. So, let's define a query to search for Italian food that is within 500 metres from the Conrad Hotel. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Italian .... OK!\n"
]
}
],
"source": [
"search_query = 'Italian'\n",
"radius = 500\n",
"print(search_query + ' .... OK!')"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Define the corresponding URL"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'https://api.foursquare.com/v2/venues/search?client_id=IZYZU23GR0ISP3CJ50HE5HTKLOURYKPY43NFKFO025ZU4I0D&client_secret=QKQ5T44KHSFQRAX32GKZFP0PWH4M53YXXC3QWA4P15ZHOQUV&ll=40.7149555,-74.0153365&v=20200101&query=Italian&radius=500&limit=30'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url = 'https://api.foursquare.com/v2/venues/search?client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}&limit={}'.format(CLIENT_ID, CLIENT_SECRET, latitude, longitude, VERSION, search_query, radius, LIMIT)\n",
"url"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Send the GET Request and examine the results"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
},
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'meta': {'code': 200, 'requestId': '5f2bcb656c66494826d3e2ea'},\n",
" 'response': {'venues': [{'id': '4fa862b3e4b0ebff2f749f06',\n",
" 'name': \"Harry's Italian Pizza Bar\",\n",
" 'location': {'address': '225 Murray St',\n",
" 'lat': 40.71521779064671,\n",
" 'lng': -74.01473940209351,\n",
" 'labeledLatLngs': [{'label': 'display',\n",
" 'lat': 40.71521779064671,\n",
" 'lng': -74.01473940209351},\n",
" {'label': 'entrance', 'lat': 40.715361, 'lng': -74.014975}],\n",
" 'distance': 58,\n",
" 'postalCode': '10282',\n",
" 'cc': 'US',\n",
" 'city': 'New York',\n",
" 'state': 'NY',\n",
" 'country': 'United States',\n",
" 'formattedAddress': ['225 Murray St',\n",
" 'New York, NY 10282',\n",
" 'United States']},\n",
" 'categories': [{'id': '4bf58dd8d48988d1ca941735',\n",
" 'name': 'Pizza Place',\n",
" 'pluralName': 'Pizza Places',\n",
" 'shortName': 'Pizza',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/pizza_',\n",
" 'suffix': '.png'},\n",
" 'primary': True}],\n",
" 'referralId': 'v-1596705612',\n",
" 'hasPerk': False},\n",
" {'id': '4f3232e219836c91c7bfde94',\n",
" 'name': 'Conca Cucina Italian Restaurant',\n",
" 'location': {'address': '63 W Broadway',\n",
" 'lat': 40.714484000000006,\n",
" 'lng': -74.00980600000001,\n",
" 'labeledLatLngs': [{'label': 'display',\n",
" 'lat': 40.714484000000006,\n",
" 'lng': -74.00980600000001}],\n",
" 'distance': 469,\n",
" 'postalCode': '10007',\n",
" 'cc': 'US',\n",
" 'city': 'New York',\n",
" 'state': 'NY',\n",
" 'country': 'United States',\n",
" 'formattedAddress': ['63 W Broadway',\n",
" 'New York, NY 10007',\n",
" 'United States']},\n",
" 'categories': [{'id': '4d4b7105d754a06374d81259',\n",
" 'name': 'Food',\n",
" 'pluralName': 'Food',\n",
" 'shortName': 'Food',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/default_',\n",
" 'suffix': '.png'},\n",
" 'primary': True}],\n",
" 'referralId': 'v-1596705612',\n",
" 'hasPerk': False},\n",
" {'id': '3fd66200f964a520f4e41ee3',\n",
" 'name': 'Ecco',\n",
" 'location': {'address': '124 Chambers St',\n",
" 'crossStreet': 'btwn Church St & W Broadway',\n",
" 'lat': 40.71533713859952,\n",
" 'lng': -74.00884766217825,\n",
" 'labeledLatLngs': [{'label': 'display',\n",
" 'lat': 40.71533713859952,\n",
" 'lng': -74.00884766217825},\n",
" {'label': 'entrance', 'lat': 40.715202, 'lng': -74.008779}],\n",
" 'distance': 549,\n",
" 'postalCode': '10007',\n",
" 'cc': 'US',\n",
" 'city': 'New York',\n",
" 'state': 'NY',\n",
" 'country': 'United States',\n",
" 'formattedAddress': ['124 Chambers St (btwn Church St & W Broadway)',\n",
" 'New York, NY 10007',\n",
" 'United States']},\n",
" 'categories': [{'id': '4bf58dd8d48988d110941735',\n",
" 'name': 'Italian Restaurant',\n",
" 'pluralName': 'Italian Restaurants',\n",
" 'shortName': 'Italian',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/italian_',\n",
" 'suffix': '.png'},\n",
" 'primary': True}],\n",
" 'referralId': 'v-1596705612',\n",
" 'hasPerk': False}]}}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results = requests.get(url).json()\n",
"results"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Get relevant part of JSON and transform it into a *pandas* dataframe"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'NoneType' object has no attribute 'items'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/IPython/core/formatters.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, obj)\u001b[0m\n\u001b[1;32m 343\u001b[0m \u001b[0mmethod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_real_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_method\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 344\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmethod\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 345\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 346\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 347\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m_repr_html_\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 732\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mbuf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetvalue\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 734\u001b[0;31m \u001b[0mmax_rows\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_option\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"display.max_rows\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 735\u001b[0m \u001b[0mmin_rows\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_option\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"display.min_rows\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 736\u001b[0m \u001b[0mmax_cols\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_option\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"display.max_columns\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/io/formats/format.py\u001b[0m in \u001b[0;36mto_html\u001b[0;34m(self, buf, encoding, classes, notebook, border)\u001b[0m\n\u001b[1;32m 980\u001b[0m \u001b[0mWhether\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mgenerated\u001b[0m \u001b[0mHTML\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mIPython\u001b[0m \u001b[0mNotebook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 981\u001b[0m \u001b[0mborder\u001b[0m \u001b[0;34m:\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 982\u001b[0;31m \u001b[0mA\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mborder\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mborder\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m \u001b[0mattribute\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mincluded\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mopening\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 983\u001b[0m \u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mtable\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m \u001b[0mtag\u001b[0m\u001b[0;34m.\u001b[0m \u001b[0mDefault\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisplay\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhtml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mborder\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 984\u001b[0m \"\"\"\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/io/formats/html.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, formatter, classes, border)\u001b[0m\n\u001b[1;32m 57\u001b[0m self.col_space = {\n\u001b[1;32m 58\u001b[0m \u001b[0mcolumn\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34mf\"{value}px\"\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 59\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mcolumn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfmt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcol_space\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 60\u001b[0m }\n\u001b[1;32m 61\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'items'"
]
},
{
"data": {
"text/plain": [
" id name \\\n",
"0 4fa862b3e4b0ebff2f749f06 Harry's Italian Pizza Bar \n",
"1 4f3232e219836c91c7bfde94 Conca Cucina Italian Restaurant \n",
"2 3fd66200f964a520f4e41ee3 Ecco \n",
"\n",
" categories referralId hasPerk \\\n",
"0 [{'id': '4bf58dd8d48988d1ca941735', 'name': 'P... v-1596705612 False \n",
"1 [{'id': '4d4b7105d754a06374d81259', 'name': 'F... v-1596705612 False \n",
"2 [{'id': '4bf58dd8d48988d110941735', 'name': 'I... v-1596705612 False \n",
"\n",
" location.address location.lat location.lng \\\n",
"0 225 Murray St 40.715218 -74.014739 \n",
"1 63 W Broadway 40.714484 -74.009806 \n",
"2 124 Chambers St 40.715337 -74.008848 \n",
"\n",
" location.labeledLatLngs location.distance \\\n",
"0 [{'label': 'display', 'lat': 40.71521779064671... 58 \n",
"1 [{'label': 'display', 'lat': 40.71448400000000... 469 \n",
"2 [{'label': 'display', 'lat': 40.71533713859952... 549 \n",
"\n",
" location.postalCode location.cc location.city location.state \\\n",
"0 10282 US New York NY \n",
"1 10007 US New York NY \n",
"2 10007 US New York NY \n",
"\n",
" location.country location.formattedAddress \\\n",
"0 United States [225 Murray St, New York, NY 10282, United Sta... \n",
"1 United States [63 W Broadway, New York, NY 10007, United Sta... \n",
"2 United States [124 Chambers St (btwn Church St & W Broadway)... \n",
"\n",
" location.crossStreet \n",
"0 NaN \n",
"1 NaN \n",
"2 btwn Church St & W Broadway "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# assign relevant part of JSON to venues\n",
"venues = results['response']['venues']\n",
"\n",
"# tranform venues into a dataframe\n",
"dataframe = json_normalize(venues)\n",
"dataframe.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Define information of interest and filter dataframe"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
},
"scrolled": true
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Series' object has no attribute '_mgr'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-f45bc30a5e1b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;31m# filter the category for each row\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mdataframe_filtered\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'categories'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdataframe_filtered\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mget_category_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;31m# clean column names by keeping only last term\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, axis, raw, result_type, args, **kwds)\u001b[0m\n\u001b[1;32m 6876\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6877\u001b[0m \u001b[0mExamples\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 6878\u001b[0;31m \u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6879\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mSingle\u001b[0m \u001b[0mlevel\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6880\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/apply.py\u001b[0m in \u001b[0;36mget_result\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 178\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_raw\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 179\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 180\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_standard\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 181\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mapply_empty_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/apply.py\u001b[0m in \u001b[0;36mapply_standard\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 253\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 254\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mapply_standard\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 255\u001b[0;31m \u001b[0mresults\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mres_index\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_series_generator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 256\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 257\u001b[0m \u001b[0;31m# wrap results\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/apply.py\u001b[0m in \u001b[0;36mapply_series_generator\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 280\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 281\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0moption_context\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"mode.chained_assignment\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 282\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseries_gen\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 283\u001b[0m \u001b[0;31m# ignore SettingWithCopy here in case the user mutates\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 284\u001b[0m \u001b[0mresults\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/apply.py\u001b[0m in \u001b[0;36mseries_generator\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 386\u001b[0m \u001b[0;31m# of it. Kids: don't do this at home.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 387\u001b[0m \u001b[0mser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_ixs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 388\u001b[0;31m \u001b[0mmgr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_mgr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 389\u001b[0m \u001b[0mblk\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mblocks\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 390\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 5272\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5273\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5274\u001b[0;31m \u001b[0mThe\u001b[0m \u001b[0mvalues\u001b[0m \u001b[0mof\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5276\u001b[0m \u001b[0mSee\u001b[0m \u001b[0mAlso\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'Series' object has no attribute '_mgr'"
]
}
],
"source": [
"# keep only columns that include venue name, and anything that is associated with location\n",
"filtered_columns = ['name', 'categories'] + [col for col in dataframe.columns if col.startswith('location.')] + ['id']\n",
"dataframe_filtered = dataframe.loc[:, filtered_columns]\n",
"\n",
"# function that extracts the category of the venue\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']\n",
"\n",
"# filter the category for each row\n",
"dataframe_filtered['categories'] = dataframe_filtered.apply(get_category_type, axis=1)\n",
"\n",
"# clean column names by keeping only last term\n",
"dataframe_filtered.columns = [column.split('.')[-1] for column in dataframe_filtered.columns]\n",
"\n",
"dataframe_filtered"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Let's visualize the Italian restaurants that are nearby"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"dataframe_filtered.name"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'DataFrame' object has no attribute 'lat'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-10-84dd12227b0f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m# add the Italian restaurants as blue circle markers\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mlat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlng\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataframe_filtered\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataframe_filtered\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlng\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataframe_filtered\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcategories\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m folium.features.CircleMarker(\n\u001b[1;32m 17\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mlat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlng\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 5272\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5273\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5274\u001b[0;31m \u001b[0mThe\u001b[0m \u001b[0mvalues\u001b[0m \u001b[0mof\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5276\u001b[0m \u001b[0mSee\u001b[0m \u001b[0mAlso\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'lat'"
]
}
],
"source": [
"venues_map = folium.Map(location=[latitude, longitude], zoom_start=13) # generate map centred around the Conrad Hotel\n",
"\n",
"# add a red circle marker to represent the Conrad Hotel\n",
"folium.features.CircleMarker(\n",
" [latitude, longitude],\n",
" radius=10,\n",
" color='red',\n",
" popup='Conrad Hotel',\n",
" fill = True,\n",
" fill_color = 'red',\n",
" fill_opacity = 0.6\n",
").add_to(venues_map)\n",
"\n",
"# add the Italian restaurants as blue circle markers\n",
"for lat, lng, label in zip(dataframe_filtered.lat, dataframe_filtered.lng, dataframe_filtered.categories):\n",
" folium.features.CircleMarker(\n",
" [lat, lng],\n",
" radius=5,\n",
" color='blue',\n",
" popup=label,\n",
" fill = True,\n",
" fill_color='blue',\n",
" fill_opacity=0.6\n",
" ).add_to(venues_map)\n",
"\n",
"# display map\n",
"venues_map"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item2\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## 2. Explore a Given Venue\n",
"> `https://api.foursquare.com/v2/venues/`**VENUE_ID**`?client_id=`**CLIENT_ID**`&client_secret=`**CLIENT_SECRET**`&v=`**VERSION**"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### A. Let's explore the closest Italian restaurant -- _Harry's Italian Pizza Bar_"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'https://api.foursquare.com/v2/venues/4fa862b3e4b0ebff2f749f06?client_id=IZYZU23GR0ISP3CJ50HE5HTKLOURYKPY43NFKFO025ZU4I0D&client_secret=QKQ5T44KHSFQRAX32GKZFP0PWH4M53YXXC3QWA4P15ZHOQUV&v=20200101'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"venue_id = '4fa862b3e4b0ebff2f749f06' # ID of Harry's Italian Pizza Bar\n",
"url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION)\n",
"url"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Send GET request for result"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['id', 'name', 'contact', 'location', 'canonicalUrl', 'categories', 'verified', 'stats', 'url', 'price', 'hasMenu', 'likes', 'dislike', 'ok', 'rating', 'ratingColor', 'ratingSignals', 'menu', 'allowMenuUrlEdit', 'beenHere', 'specials', 'photos', 'reasons', 'hereNow', 'createdAt', 'tips', 'shortUrl', 'timeZone', 'listed', 'hours', 'popular', 'seasonalHours', 'defaultHours', 'pageUpdates', 'inbox', 'attributes', 'bestPhoto', 'colors'])\n"
]
},
{
"data": {
"text/plain": [
"{'id': '4fa862b3e4b0ebff2f749f06',\n",
" 'name': \"Harry's Italian Pizza Bar\",\n",
" 'contact': {'phone': '2126081007', 'formattedPhone': '(212) 608-1007'},\n",
" 'location': {'address': '225 Murray St',\n",
" 'lat': 40.71521779064671,\n",
" 'lng': -74.01473940209351,\n",
" 'labeledLatLngs': [{'label': 'display',\n",
" 'lat': 40.71521779064671,\n",
" 'lng': -74.01473940209351},\n",
" {'label': 'entrance', 'lat': 40.715361, 'lng': -74.014975}],\n",
" 'postalCode': '10282',\n",
" 'cc': 'US',\n",
" 'city': 'New York',\n",
" 'state': 'NY',\n",
" 'country': 'United States',\n",
" 'formattedAddress': ['225 Murray St',\n",
" 'New York, NY 10282',\n",
" 'United States']},\n",
" 'canonicalUrl': 'https://foursquare.com/v/harrys-italian-pizza-bar/4fa862b3e4b0ebff2f749f06',\n",
" 'categories': [{'id': '4bf58dd8d48988d1ca941735',\n",
" 'name': 'Pizza Place',\n",
" 'pluralName': 'Pizza Places',\n",
" 'shortName': 'Pizza',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/pizza_',\n",
" 'suffix': '.png'},\n",
" 'primary': True},\n",
" {'id': '4bf58dd8d48988d110941735',\n",
" 'name': 'Italian Restaurant',\n",
" 'pluralName': 'Italian Restaurants',\n",
" 'shortName': 'Italian',\n",
" 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/italian_',\n",
" 'suffix': '.png'}}],\n",
" 'verified': False,\n",
" 'stats': {'tipCount': 57},\n",
" 'url': 'http://harrysitalian.com',\n",
" 'price': {'tier': 2, 'message': 'Moderate', 'currency': '$'},\n",
" 'hasMenu': True,\n",
" 'likes': {'count': 120,\n",
" 'groups': [{'type': 'others', 'count': 120, 'items': []}],\n",
" 'summary': '120 Likes'},\n",
" 'dislike': False,\n",
" 'ok': False,\n",
" 'rating': 6.9,\n",
" 'ratingColor': 'FFC800',\n",
" 'ratingSignals': 212,\n",
" 'menu': {'type': 'Menu',\n",
" 'label': 'Menu',\n",
" 'anchor': 'View Menu',\n",
" 'url': 'https://foursquare.com/v/harrys-italian-pizza-bar/4fa862b3e4b0ebff2f749f06/menu',\n",
" 'mobileUrl': 'https://foursquare.com/v/4fa862b3e4b0ebff2f749f06/device_menu'},\n",
" 'allowMenuUrlEdit': True,\n",
" 'beenHere': {'count': 0,\n",
" 'unconfirmedCount': 0,\n",
" 'marked': False,\n",
" 'lastCheckinExpiredAt': 0},\n",
" 'specials': {'count': 0, 'items': []},\n",
" 'photos': {'count': 146,\n",
" 'groups': [{'type': 'venue',\n",
" 'name': 'Venue photos',\n",
" 'count': 146,\n",
" 'items': [{'id': '4fad980de4b091b4626c3633',\n",
" 'createdAt': 1336776717,\n",
" 'source': {'name': 'Foursquare for Android',\n",
" 'url': 'https://foursquare.com/download/#/android'},\n",
" 'prefix': 'https://fastly.4sqi.net/img/general/',\n",
" 'suffix': '/ya1iQFI7pLjuIJp1PGDKlrZS3OJdHCF7tpILMmjv_2w.jpg',\n",
" 'width': 480,\n",
" 'height': 640,\n",
" 'user': {'id': '13676709',\n",
" 'firstName': 'Leony',\n",
" 'lastName': 'N',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/T0ANFNGNMCHUDEUE.jpg'}},\n",
" 'visibility': 'public'}]}]},\n",
" 'reasons': {'count': 1,\n",
" 'items': [{'summary': 'Lots of people like this place',\n",
" 'type': 'general',\n",
" 'reasonName': 'rawLikesReason'}]},\n",
" 'hereNow': {'count': 0, 'summary': 'Nobody here', 'groups': []},\n",
" 'createdAt': 1336435379,\n",
" 'tips': {'count': 57,\n",
" 'groups': [{'type': 'others',\n",
" 'name': 'All tips',\n",
" 'count': 57,\n",
" 'items': [{'id': '53d27909498e0523841340b6',\n",
" 'createdAt': 1406302473,\n",
" 'text': \"Harry's Italian Pizza bar is known for it's amazing pizza, but did you know that the brunches here are amazing too? Try the Nutella French toast and we know you'll be sold.\",\n",
" 'type': 'user',\n",
" 'canonicalUrl': 'https://foursquare.com/item/53d27909498e0523841340b6',\n",
" 'lang': 'en',\n",
" 'likes': {'count': 4,\n",
" 'groups': [{'type': 'others',\n",
" 'count': 4,\n",
" 'items': [{'id': '369426',\n",
" 'firstName': 'P.',\n",
" 'lastName': 'M',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/JPQYUWJKUT0H2OO4.jpg'}},\n",
" {'id': '87587879',\n",
" 'firstName': 'Diane',\n",
" 'lastName': 'D',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/87587879-ESLRSZLQ2CBE2P4W.jpg'}},\n",
" {'id': '87591341',\n",
" 'firstName': 'Tim',\n",
" 'lastName': 'S',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/-Z4YK4VKE0JSVXIY1.jpg'}},\n",
" {'id': '87473404',\n",
" 'firstName': 'TenantKing.com',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/87473404-HI5DTBTK0HX401CA.png'},\n",
" 'type': 'page'}]}],\n",
" 'summary': '4 likes'},\n",
" 'logView': True,\n",
" 'agreeCount': 4,\n",
" 'disagreeCount': 0,\n",
" 'todo': {'count': 0},\n",
" 'user': {'id': '87473404',\n",
" 'firstName': 'TenantKing.com',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/87473404-HI5DTBTK0HX401CA.png'},\n",
" 'type': 'page'}}]}]},\n",
" 'shortUrl': 'http://4sq.com/JNblHV',\n",
" 'timeZone': 'America/New_York',\n",
" 'listed': {'count': 54,\n",
" 'groups': [{'type': 'others',\n",
" 'name': 'Lists from other people',\n",
" 'count': 54,\n",
" 'items': [{'id': '4fa32fd0e4b04193744746b1',\n",
" 'name': 'Manhattan Haunts',\n",
" 'description': '',\n",
" 'type': 'others',\n",
" 'user': {'id': '24592223',\n",
" 'firstName': 'Becca',\n",
" 'lastName': 'M',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/24592223-RAW2UYM0GIB1U40K.jpg'}},\n",
" 'editable': False,\n",
" 'public': True,\n",
" 'collaborative': False,\n",
" 'url': '/becca_mcarthur/list/manhattan-haunts',\n",
" 'canonicalUrl': 'https://foursquare.com/becca_mcarthur/list/manhattan-haunts',\n",
" 'createdAt': 1336094672,\n",
" 'updatedAt': 1380845377,\n",
" 'photo': {'id': '4e8cc9461081e3b3544e12e5',\n",
" 'createdAt': 1317849414,\n",
" 'prefix': 'https://fastly.4sqi.net/img/general/',\n",
" 'suffix': '/0NLVU2HC1JF4DXIMKWUFW3QBUT31DC11EFNYYHMJG3NDWAPS.jpg',\n",
" 'width': 492,\n",
" 'height': 330,\n",
" 'user': {'id': '742542',\n",
" 'firstName': 'Time Out New York',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/XXHKCBSQHBORZNSR.jpg'},\n",
" 'type': 'page'},\n",
" 'visibility': 'public'},\n",
" 'followers': {'count': 22},\n",
" 'listItems': {'count': 187,\n",
" 'items': [{'id': 'v4fa862b3e4b0ebff2f749f06',\n",
" 'createdAt': 1342934485}]}},\n",
" {'id': '4fae817be4b085f6b2a74d19',\n",
" 'name': 'USA NYC MAN FiDi',\n",
" 'description': 'Where to go for decent eats in the restaurant wasteland of Downtown NYC aka FiDi, along with Tribeca & Battery Park City.',\n",
" 'type': 'others',\n",
" 'user': {'id': '12113441',\n",
" 'firstName': 'Kino',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/12113441-K5HTHFLU2MUCM0CM.jpg'}},\n",
" 'editable': False,\n",
" 'public': True,\n",
" 'collaborative': False,\n",
" 'url': '/kinosfault/list/usa-nyc-man-fidi',\n",
" 'canonicalUrl': 'https://foursquare.com/kinosfault/list/usa-nyc-man-fidi',\n",
" 'createdAt': 1336836475,\n",
" 'updatedAt': 1556754919,\n",
" 'photo': {'id': '55984992498e13ba75e353bb',\n",
" 'createdAt': 1436043666,\n",
" 'prefix': 'https://fastly.4sqi.net/img/general/',\n",
" 'suffix': '/12113441_iOa6Uh-Xi8bhj2-gpzkkw8MKiAIs7RmOcz_RM7m8ink.jpg',\n",
" 'width': 540,\n",
" 'height': 960,\n",
" 'user': {'id': '12113441',\n",
" 'firstName': 'Kino',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/12113441-K5HTHFLU2MUCM0CM.jpg'}},\n",
" 'visibility': 'public'},\n",
" 'followers': {'count': 20},\n",
" 'listItems': {'count': 273,\n",
" 'items': [{'id': 'v4fa862b3e4b0ebff2f749f06',\n",
" 'createdAt': 1373909433}]}},\n",
" {'id': '4fddeff0e4b0e078037ac0d3',\n",
" 'name': 'NYC Resturants',\n",
" 'description': '',\n",
" 'type': 'others',\n",
" 'user': {'id': '21563126',\n",
" 'firstName': 'Richard',\n",
" 'lastName': 'R',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/21563126_v05J1KPw_SVj6Ehq9g8B9jeAGjFUMsU5QGl-NZ8inUQ7pKQm5bKplW37EmR7jS2A7GYPBBAtl.jpg'}},\n",
" 'editable': False,\n",
" 'public': True,\n",
" 'collaborative': True,\n",
" 'url': '/rickr7/list/nyc-resturants',\n",
" 'canonicalUrl': 'https://foursquare.com/rickr7/list/nyc-resturants',\n",
" 'createdAt': 1339944944,\n",
" 'updatedAt': 1591664261,\n",
" 'photo': {'id': '5072dd13e4b09145cdf782d1',\n",
" 'createdAt': 1349704979,\n",
" 'prefix': 'https://fastly.4sqi.net/img/general/',\n",
" 'suffix': '/208205_fGh2OuAZ9qJ4agbAA5wMVNOSIm9kNUlRtNwj1N-adqg.jpg',\n",
" 'width': 800,\n",
" 'height': 800,\n",
" 'user': {'id': '208205',\n",
" 'firstName': 'Thalia',\n",
" 'lastName': 'K',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/SNOOLCAW2AG04ZKD.jpg'}},\n",
" 'visibility': 'public'},\n",
" 'followers': {'count': 12},\n",
" 'listItems': {'count': 193,\n",
" 'items': [{'id': 'v4fa862b3e4b0ebff2f749f06',\n",
" 'createdAt': 1581655865}]}},\n",
" {'id': '5266c68a498e7c667807fe09',\n",
" 'name': 'Foodie Love in NY - 02',\n",
" 'description': '',\n",
" 'type': 'others',\n",
" 'user': {'id': '547977',\n",
" 'firstName': 'WiLL',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/-Q5NYGDMFDMOITQRR.jpg'}},\n",
" 'editable': False,\n",
" 'public': True,\n",
" 'collaborative': False,\n",
" 'url': '/sweetiewill/list/foodie-love-in-ny--02',\n",
" 'canonicalUrl': 'https://foursquare.com/sweetiewill/list/foodie-love-in-ny--02',\n",
" 'createdAt': 1382467210,\n",
" 'updatedAt': 1391995585,\n",
" 'followers': {'count': 7},\n",
" 'listItems': {'count': 200,\n",
" 'items': [{'id': 'v4fa862b3e4b0ebff2f749f06',\n",
" 'createdAt': 1386809936}]}}]}]},\n",
" 'hours': {'status': 'Closed until 11:30 AM',\n",
" 'richStatus': {'entities': [], 'text': 'Closed until 11:30 AM'},\n",
" 'isOpen': False,\n",
" 'isLocalHoliday': False,\n",
" 'dayData': [],\n",
" 'timeframes': [{'days': 'Mon–Wed, Sun',\n",
" 'open': [{'renderedTime': '11:30 AM–11:00 PM'}],\n",
" 'segments': []},\n",
" {'days': 'Thu–Sat',\n",
" 'includesToday': True,\n",
" 'open': [{'renderedTime': '11:30 AM–Midnight'}],\n",
" 'segments': []}]},\n",
" 'popular': {'isOpen': False,\n",
" 'isLocalHoliday': False,\n",
" 'timeframes': [{'days': 'Today',\n",
" 'includesToday': True,\n",
" 'open': [{'renderedTime': 'Noon–2:00 PM'},\n",
" {'renderedTime': '5:00 PM–10:00 PM'}],\n",
" 'segments': []},\n",
" {'days': 'Fri',\n",
" 'open': [{'renderedTime': 'Noon–3:00 PM'},\n",
" {'renderedTime': '5:00 PM–11:00 PM'}],\n",
" 'segments': []},\n",
" {'days': 'Sat',\n",
" 'open': [{'renderedTime': 'Noon–11:00 PM'}],\n",
" 'segments': []},\n",
" {'days': 'Sun',\n",
" 'open': [{'renderedTime': 'Noon–3:00 PM'},\n",
" {'renderedTime': '5:00 PM–8:00 PM'}],\n",
" 'segments': []},\n",
" {'days': 'Mon',\n",
" 'open': [{'renderedTime': 'Noon–2:00 PM'},\n",
" {'renderedTime': '6:00 PM–8:00 PM'}],\n",
" 'segments': []},\n",
" {'days': 'Tue–Wed',\n",
" 'open': [{'renderedTime': 'Noon–2:00 PM'},\n",
" {'renderedTime': '5:00 PM–10:00 PM'}],\n",
" 'segments': []}]},\n",
" 'seasonalHours': [],\n",
" 'defaultHours': {'status': 'Closed until 11:30 AM',\n",
" 'richStatus': {'entities': [], 'text': 'Closed until 11:30 AM'},\n",
" 'isOpen': False,\n",
" 'isLocalHoliday': False,\n",
" 'dayData': [],\n",
" 'timeframes': [{'days': 'Mon–Wed, Sun',\n",
" 'open': [{'renderedTime': '11:30 AM–11:00 PM'}],\n",
" 'segments': []},\n",
" {'days': 'Thu–Sat',\n",
" 'includesToday': True,\n",
" 'open': [{'renderedTime': '11:30 AM–Midnight'}],\n",
" 'segments': []}]},\n",
" 'pageUpdates': {'count': 0, 'items': []},\n",
" 'inbox': {'count': 0, 'items': []},\n",
" 'attributes': {'groups': [{'type': 'price',\n",
" 'name': 'Price',\n",
" 'summary': '$$',\n",
" 'count': 1,\n",
" 'items': [{'displayName': 'Price', 'displayValue': '$$', 'priceTier': 2}]},\n",
" {'type': 'payments',\n",
" 'name': 'Credit Cards',\n",
" 'summary': 'Credit Cards',\n",
" 'count': 7,\n",
" 'items': [{'displayName': 'Credit Cards',\n",
" 'displayValue': 'Yes (incl. American Express)'}]},\n",
" {'type': 'outdoorSeating',\n",
" 'name': 'Outdoor Seating',\n",
" 'summary': 'Outdoor Seating',\n",
" 'count': 1,\n",
" 'items': [{'displayName': 'Outdoor Seating', 'displayValue': 'Yes'}]},\n",
" {'type': 'serves',\n",
" 'name': 'Menus',\n",
" 'summary': 'Happy Hour, Brunch & more',\n",
" 'count': 8,\n",
" 'items': [{'displayName': 'Brunch', 'displayValue': 'Brunch'},\n",
" {'displayName': 'Lunch', 'displayValue': 'Lunch'},\n",
" {'displayName': 'Dinner', 'displayValue': 'Dinner'},\n",
" {'displayName': 'Happy Hour', 'displayValue': 'Happy Hour'}]},\n",
" {'type': 'drinks',\n",
" 'name': 'Drinks',\n",
" 'summary': 'Beer, Wine & Cocktails',\n",
" 'count': 5,\n",
" 'items': [{'displayName': 'Beer', 'displayValue': 'Beer'},\n",
" {'displayName': 'Wine', 'displayValue': 'Wine'},\n",
" {'displayName': 'Cocktails', 'displayValue': 'Cocktails'}]}]},\n",
" 'bestPhoto': {'id': '4fad980de4b091b4626c3633',\n",
" 'createdAt': 1336776717,\n",
" 'source': {'name': 'Foursquare for Android',\n",
" 'url': 'https://foursquare.com/download/#/android'},\n",
" 'prefix': 'https://fastly.4sqi.net/img/general/',\n",
" 'suffix': '/ya1iQFI7pLjuIJp1PGDKlrZS3OJdHCF7tpILMmjv_2w.jpg',\n",
" 'width': 480,\n",
" 'height': 640,\n",
" 'visibility': 'public'},\n",
" 'colors': {'highlightColor': {'photoId': '4fad980de4b091b4626c3633',\n",
" 'value': -13619152},\n",
" 'highlightTextColor': {'photoId': '4fad980de4b091b4626c3633', 'value': -1},\n",
" 'algoVersion': 3}}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result = requests.get(url).json()\n",
"print(result['response']['venue'].keys())\n",
"result['response']['venue']"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### B. Get the venue's overall rating"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.9\n"
]
}
],
"source": [
"try:\n",
" print(result['response']['venue']['rating'])\n",
"except:\n",
" print('This venue has not been rated yet.')"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"That is not a very good rating. Let's check the rating of the second closest Italian restaurant."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This venue has not been rated yet.\n"
]
}
],
"source": [
"venue_id = '4f3232e219836c91c7bfde94' # ID of Conca Cucina Italian Restaurant\n",
"url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION)\n",
"\n",
"result = requests.get(url).json()\n",
"try:\n",
" print(result['response']['venue']['rating'])\n",
"except:\n",
" print('This venue has not been rated yet.')"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Since this restaurant has no ratings, let's check the third restaurant."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.3\n"
]
}
],
"source": [
"venue_id = '3fd66200f964a520f4e41ee3' # ID of Ecco\n",
"url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION)\n",
"\n",
"result = requests.get(url).json()\n",
"try:\n",
" print(result['response']['venue']['rating'])\n",
"except:\n",
" print('This venue has not been rated yet.')"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Since this restaurant has a slightly better rating, let's explore it further."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### C. Get the number of tips"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"19"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result['response']['venue']['tips']['count']"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### D. Get the venue's tips\n",
"> `https://api.foursquare.com/v2/venues/`**VENUE_ID**`/tips?client_id=`**CLIENT_ID**`&client_secret=`**CLIENT_SECRET**`&v=`**VERSION**`&limit=`**LIMIT**"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Create URL and send GET request. Make sure to set limit to get all tips"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'meta': {'code': 200, 'requestId': '5f2bcb872b6bc220ae242436'},\n",
" 'response': {'tips': {'count': 19,\n",
" 'items': [{'id': '5ab1cb46c9a517174651d3fe',\n",
" 'createdAt': 1521601350,\n",
" 'text': 'A+ Italian food! Trust me on this: my mom’s side of the family is 100% Italian. I was born and bred to know good pasta when I see it, and Ecco is one of my all-time NYC favorites',\n",
" 'type': 'user',\n",
" 'canonicalUrl': 'https://foursquare.com/item/5ab1cb46c9a517174651d3fe',\n",
" 'lang': 'en',\n",
" 'likes': {'count': 0, 'groups': []},\n",
" 'logView': True,\n",
" 'agreeCount': 4,\n",
" 'disagreeCount': 0,\n",
" 'todo': {'count': 0},\n",
" 'user': {'id': '484542633',\n",
" 'firstName': 'Nick',\n",
" 'lastName': 'E',\n",
" 'photo': {'prefix': 'https://fastly.4sqi.net/img/user/',\n",
" 'suffix': '/484542633_unymNUmw_FdPs3GjXHujmHcYnN4hf8kEPADlOZuIrdcdm97VX3tFqL7fFNMNA_8Gl9NlU1GYg.jpg'}},\n",
" 'authorInteractionType': 'liked'}]}}}"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## Ecco Tips\n",
"limit = 15 # set limit to be greater than or equal to the total number of tips\n",
"url = 'https://api.foursquare.com/v2/venues/{}/tips?client_id={}&client_secret={}&v={}&limit={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION, limit)\n",
"\n",
"results = requests.get(url).json()\n",
"results"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Get tips and list of associated features"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['id', 'createdAt', 'text', 'type', 'canonicalUrl', 'lang', 'likes', 'logView', 'agreeCount', 'disagreeCount', 'todo', 'user', 'authorInteractionType'])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tips = results['response']['tips']['items']\n",
"\n",
"tip = results['response']['tips']['items'][0]\n",
"tip.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Format column width and display all tips"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: Passing a negative integer is deprecated in version 1.0 and will not be supported in future version. Instead, use None to not limit the column width.\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"ename": "KeyError",
"evalue": "'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-19-fe9c7711676c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# columns to keep\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mfiltered_columns\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'text'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'agreeCount'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'disagreeCount'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'id'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'user.firstName'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'user.lastName'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'user.gender'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'user.id'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mtips_filtered\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtips_df\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfiltered_columns\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;31m# display tips\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 1760\u001b[0m \u001b[0misetter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1761\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1762\u001b[0;31m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1763\u001b[0m \u001b[0;31m# scalar value\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1764\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mloc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0milocs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_tuple\u001b[0;34m(self, tup)\u001b[0m\n\u001b[1;32m 1287\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1288\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1289\u001b[0;31m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1290\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1291\u001b[0m \u001b[0;31m# Count missing values:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 1952\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0max\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1953\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1954\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Incompatible indexer with Series\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1955\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1956\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_align_frame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mABCDataFrame\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_iterable\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 1593\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1594\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1595\u001b[0;31m \u001b[0;31m# add a new item with the dtype setup\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1596\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_infer_fill_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1597\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_get_listlike_indexer\u001b[0;34m(self, key, axis, raise_missing)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;31m# if we have any multi-indexes that have non-trivial slices\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;31m# (not null slices) then we must take the split path, xref\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;31m# GH 10360, GH 27841\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maxes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_validate_read_indexer\u001b[0;34m(self, key, indexer, axis, raise_missing)\u001b[0m\n\u001b[1;32m 1653\u001b[0m \u001b[0milocs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mri\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0minfo_idx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1654\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1655\u001b[0;31m \u001b[0mplane_indexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1656\u001b[0m \u001b[0mlplane_indexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlength_of_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplane_indexer\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1657\u001b[0m \u001b[0;31m# lplane_indexer gives the expected length of obj[indexer[0]]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'"
]
}
],
"source": [
"pd.set_option('display.max_colwidth', -1)\n",
"\n",
"tips_df = json_normalize(tips) # json normalize tips\n",
"\n",
"# columns to keep\n",
"filtered_columns = ['text', 'agreeCount', 'disagreeCount', 'id', 'user.firstName', 'user.lastName', 'user.gender', 'user.id']\n",
"tips_filtered = tips_df.loc[:, filtered_columns]\n",
"\n",
"# display tips\n",
"tips_filtered"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Now remember that because we are using a personal developer account, then we can access only 2 of the restaurant's tips, instead of all 15 tips."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item3\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## 3. Search a Foursquare User\n",
"> `https://api.foursquare.com/v2/users/`**USER_ID**`?client_id=`**CLIENT_ID**`&client_secret=`**CLIENT_SECRET**`&v=`**VERSION**"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Define URL, send GET request and display features associated with user"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"user_id = '484542633' # user ID with most agree counts and complete profile\n",
"\n",
"url = 'https://api.foursquare.com/v2/users/{}?client_id={}&client_secret={}&v={}'.format(user_id, CLIENT_ID, CLIENT_SECRET, VERSION) # define URL\n",
"\n",
"# send GET request\n",
"results = requests.get(url).json()\n",
"user_data = results['response']['user']\n",
"\n",
"# display features associated with user\n",
"user_data.keys()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"print('First Name: ' + user_data['firstName'])\n",
"print('Last Name: ' + user_data['lastName'])\n",
"print('Home City: ' + user_data['homeCity'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### How many tips has this user submitted?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"user_data['tips']"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Wow! So it turns out that Nick is a very active Foursquare user, with more than 250 tips."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Get User's tips"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"# define tips URL\n",
"url = 'https://api.foursquare.com/v2/users/{}/tips?client_id={}&client_secret={}&v={}&limit={}'.format(user_id, CLIENT_ID, CLIENT_SECRET, VERSION, limit)\n",
"\n",
"# send GET request and get user's tips\n",
"results = requests.get(url).json()\n",
"tips = results['response']['tips']['items']\n",
"\n",
"# format column width\n",
"pd.set_option('display.max_colwidth', -1)\n",
"\n",
"tips_df = json_normalize(tips)\n",
"\n",
"# filter columns\n",
"filtered_columns = ['text', 'agreeCount', 'disagreeCount', 'id']\n",
"tips_filtered = tips_df.loc[:, filtered_columns]\n",
"\n",
"# display user's tips\n",
"tips_filtered"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Let's get the venue for the tip with the greatest number of agree counts"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"tip_id = '5ab5575d73fe2516ad8f363b' # tip id\n",
"\n",
"# define URL\n",
"url = 'http://api.foursquare.com/v2/tips/{}?client_id={}&client_secret={}&v={}'.format(tip_id, CLIENT_ID, CLIENT_SECRET, VERSION)\n",
"\n",
"# send GET Request and examine results\n",
"result = requests.get(url).json()\n",
"print(result['response']['tip']['venue']['name'])\n",
"print(result['response']['tip']['venue']['location'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Get User's friends"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"user_friends = json_normalize(user_data['friends']['groups'][0]['items'])\n",
"user_friends"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Interesting. Despite being very active, it turns out that Nick does not have any friends on Foursquare. This might definitely change in the future."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Retrieve the User's Profile Image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
},
"scrolled": true
},
"outputs": [],
"source": [
"user_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"# 1. grab prefix of photo\n",
"# 2. grab suffix of photo\n",
"# 3. concatenate them using the image size \n",
"Image(url='https://igx.4sqi.net/img/user/300x300/484542633_mK2Yum7T_7Tn9fWpndidJsmw2Hof_6T5vJBKCHPLMK5OL-U5ZiJGj51iwBstcpDLYa3Zvhvis.jpg')"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item4\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## 4. Explore a location\n",
"> `https://api.foursquare.com/v2/venues/`**explore**`?client_id=`**CLIENT_ID**`&client_secret=`**CLIENT_SECRET**`&ll=`**LATITUDE**`,`**LONGITUDE**`&v=`**VERSION**`&limit=`**LIMIT**"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### So, you just finished your gourmet dish at Ecco, and are just curious about the popular spots around the restaurant. In order to explore the area, let's start by getting the latitude and longitude values of Ecco Restaurant."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": true,
"deletable": true,
"jupyter": {
"outputs_hidden": true
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"latitude = 40.715337\n",
"longitude = -74.008848"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Define URL"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"url = 'https://api.foursquare.com/v2/venues/explore?client_id={}&client_secret={}&ll={},{}&v={}&radius={}&limit={}'.format(CLIENT_ID, CLIENT_SECRET, latitude, longitude, VERSION, radius, LIMIT)\n",
"url"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Send GET request and examine results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": true,
"deletable": true,
"jupyter": {
"outputs_hidden": true
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"import requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"results = requests.get(url).json()\n",
"'There are {} around Ecco restaurant.'.format(len(results['response']['groups'][0]['items']))"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Get relevant part of JSON"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"items = results['response']['groups'][0]['items']\n",
"items[0]"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Process JSON and convert it to a clean dataframe"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"dataframe = json_normalize(items) # flatten JSON\n",
"\n",
"# filter columns\n",
"filtered_columns = ['venue.name', 'venue.categories'] + [col for col in dataframe.columns if col.startswith('venue.location.')] + ['venue.id']\n",
"dataframe_filtered = dataframe.loc[:, filtered_columns]\n",
"\n",
"# filter the category for each row\n",
"dataframe_filtered['venue.categories'] = dataframe_filtered.apply(get_category_type, axis=1)\n",
"\n",
"# clean columns\n",
"dataframe_filtered.columns = [col.split('.')[-1] for col in dataframe_filtered.columns]\n",
"\n",
"dataframe_filtered.head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Let's visualize these items on the map around our location"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"venues_map = folium.Map(location=[latitude, longitude], zoom_start=15) # generate map centred around Ecco\n",
"\n",
"\n",
"# add Ecco as a red circle mark\n",
"folium.features.CircleMarker(\n",
" [latitude, longitude],\n",
" radius=10,\n",
" popup='Ecco',\n",
" fill=True,\n",
" color='red',\n",
" fill_color='red',\n",
" fill_opacity=0.6\n",
" ).add_to(venues_map)\n",
"\n",
"\n",
"# add popular spots to the map as blue circle markers\n",
"for lat, lng, label in zip(dataframe_filtered.lat, dataframe_filtered.lng, dataframe_filtered.categories):\n",
" folium.features.CircleMarker(\n",
" [lat, lng],\n",
" radius=5,\n",
" popup=label,\n",
" fill=True,\n",
" color='blue',\n",
" fill_color='blue',\n",
" fill_opacity=0.6\n",
" ).add_to(venues_map)\n",
"\n",
"# display map\n",
"venues_map"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item5\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## 5. Explore Trending Venues\n",
"> `https://api.foursquare.com/v2/venues/`**trending**`?client_id=`**CLIENT_ID**`&client_secret=`**CLIENT_SECRET**`&ll=`**LATITUDE**`,`**LONGITUDE**`&v=`**VERSION**"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Now, instead of simply exploring the area around Ecco, you are interested in knowing the venues that are trending at the time you are done with your lunch, meaning the places with the highest foot traffic. So let's do that and get the trending venues around Ecco."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"# define URL\n",
"url = 'https://api.foursquare.com/v2/venues/trending?client_id={}&client_secret={}&ll={},{}&v={}'.format(CLIENT_ID, CLIENT_SECRET, latitude, longitude, VERSION)\n",
"\n",
"# send GET request and get trending venues\n",
"results = requests.get(url).json()\n",
"results"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Check if any venues are trending at this time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": true,
"deletable": true,
"jupyter": {
"outputs_hidden": true
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"if len(results['response']['venues']) == 0:\n",
" trending_venues_df = 'No trending venues are available at the moment!'\n",
" \n",
"else:\n",
" trending_venues = results['response']['venues']\n",
" trending_venues_df = json_normalize(trending_venues)\n",
"\n",
" # filter columns\n",
" columns_filtered = ['name', 'categories'] + ['location.distance', 'location.city', 'location.postalCode', 'location.state', 'location.country', 'location.lat', 'location.lng']\n",
" trending_venues_df = trending_venues_df.loc[:, columns_filtered]\n",
"\n",
" # filter the category for each row\n",
" trending_venues_df['categories'] = trending_venues_df.apply(get_category_type, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"# display trending venues\n",
"trending_venues_df"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Now, depending on when you run the above code, you might get different venues since the venues with the highest foot traffic are fetched live. "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Visualize trending venues"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"if len(results['response']['venues']) == 0:\n",
" venues_map = 'Cannot generate visual as no trending venues are available at the moment!'\n",
"\n",
"else:\n",
" venues_map = folium.Map(location=[latitude, longitude], zoom_start=15) # generate map centred around Ecco\n",
"\n",
"\n",
" # add Ecco as a red circle mark\n",
" folium.features.CircleMarker(\n",
" [latitude, longitude],\n",
" radius=10,\n",
" popup='Ecco',\n",
" fill=True,\n",
" color='red',\n",
" fill_color='red',\n",
" fill_opacity=0.6\n",
" ).add_to(venues_map)\n",
"\n",
"\n",
" # add the trending venues as blue circle markers\n",
" for lat, lng, label in zip(trending_venues_df['location.lat'], trending_venues_df['location.lng'], trending_venues_df['name']):\n",
" folium.features.CircleMarker(\n",
" [lat, lng],\n",
" radius=5,\n",
" poup=label,\n",
" fill=True,\n",
" color='blue',\n",
" fill_color='blue',\n",
" fill_opacity=0.6\n",
" ).add_to(venues_map)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"button": false,
"collapsed": false,
"deletable": true,
"jupyter": {
"outputs_hidden": false
},
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"# display map\n",
"venues_map"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item6\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Thank you for completing this lab!\n",
"\n",
"This notebook was created by [Alex Aklson](https://www.linkedin.com/in/aklson/). I hope you found this lab interesting and educational. Feel free to contact me if you have any questions!"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"This notebook is part of a course on **Coursera** called *Applied Data Science Capstone*. If you accessed this notebook outside the course, you can take this course online by clicking [here](http://cocl.us/DP0701EN_Coursera_Week2_LAB1)."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<hr>\n",
"Copyright &copy; 2018 [Cognitive Class](https://cognitiveclass.ai/?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/)."
]
}
],
"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.11"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment