Browse the portal for public datasets: https://data.sfgov.org/
Use Python to retrieve data from the API, just like we learned last week: https://data.sfgov.org/developers
Look at data on bike parking in SF: https://data.sfgov.org/Transportation/Bicycle-Parking-Public-/2e7e-i7me
In [1]:
import pandas as pd, requests, json
In [2]:
# use endpoint for bike parking in SF
endpoint_url = 'https://data.sfgov.org/resource/dd7x-3h4a.json'
# open a connection to the URL and download its response
response = requests.get(endpoint_url)
# parse the json string into a Python dict
data = response.json()
In [3]:
data[0]
Out[3]:
In [4]:
# turn the json data into a dataframe
df = pd.DataFrame(data)
df.head()
Out[4]:
In [5]:
# this column contains dicts that contain lats and lons
df['geom'][1]
Out[5]:
In [6]:
# create lists to hold all my lat-lons as i extract them
latitudes = []
longitudes = []
# loop through each row in df, extracting lat and lon values if geom is not null
for label, row in df.iterrows():
if pd.notnull(row['geom']):
latitudes.append(row['geom']['coordinates'][1])
longitudes.append(row['geom']['coordinates'][0])
else:
latitudes.append(None)
longitudes.append(None)
In [7]:
df['lat'] = latitudes
df['lon'] = longitudes
In [8]:
df2 = df[['lat', 'lon']]
df2.head()
Out[8]:
In [ ]: