In [1]:
# Import libraries.
import csv
import httplib2
from apiclient.discovery import build
import urllib
import json
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
In [3]:
# This API key is provided by google as described in the tutorial
API_KEY = 'AIzaSyCuKKwcT5mUYZP_Q-heqCPjvdWyacRuX00'
# This is the table id for the fusion table
TABLE_ID = '1ymz3EtGdi4qKGMl5AxEFXtTlgk3tKi8iCpjTzvM'
# open the data stored in a file called "data.json"
try:
fp = open("data.json")
response = json.load(fp)
# but if that file does not exist, download the data from fusiontables
except IOError:
service = build('fusiontables', 'v1', developerKey=API_KEY)
query = "SELECT * FROM " + TABLE_ID + " WHERE AnimalType = 'DOG'"
response = service.query().sql(sql=query).execute()
fp = open("data.json", "w")
json.dump(response, fp)
In [4]:
# Check how many rows we have.
print len(response['rows'])
In [5]:
data_df = pd.DataFrame(response[u'rows'], columns = response[u'columns'])
In [6]:
# Show a few rows.
data_df.head()
Out[6]:
In [22]:
lat_lon_df = data_df[['Latitude', 'Longitude']]
lat_lon_df.head()
Out[22]:
Now we can quickly convert both columns to numeric and remove values that could not be converted (NaN). It is all done on one line!
In [19]:
lat_lon_df = lat_lon_df.apply(lambda x: pd.to_numeric(x, errors='ignore')).dropna()
In [21]:
sns.pairplot(lat_lon_df)
Out[21]:
In [ ]: