In: Computer Science
Going through a basic tutorial on bringing in data from a URL in Python. So far, I've imported Citi Bike station data and converted the JSON file to a Python dictionary named datadict...
import requests
response =
requests.get("https://gbfs.citibikenyc.com/gbfs/en/station_information.json")
if response.status_code != 200:
print("Error with website. If problem persists, contact your
Facilitator!")
else:
print("Data download successful")
datadict = response.json()
print(datadict.keys())
print(datadict['data'].keys())
datadict['data']['stations']
The task is to now...
In the code cell below, write and evaluate code to extract the latitude and longitude of every bike station, and store the result in a variable named coordinates. Store the data in a numpy array of shape (N,2), where N is the total number of stations, and 2 reflects the number of columns — store all the longitudes in the first column and all the latitudes in the second column.
Carry out this data extraction however you see fit. The basic steps you will need to carry out are:
After importing numpy, the remaining set of steps above can be done in one line, using a list comprehension to extract the desired pair of fields from each station entry and then converting the list to a numpy array using the np.array function. But if you'd rather break out each step separately within a for loop, that would work too. When you are finished, you should have a numpy array with shape approximately equal to (1000, 2), since there should be approximately 1000 stations in the full array of coordinates (although that number can change over time as new stations are added or removed). Print the shape of the array to determine how many stations are included in the dataset that you downloaded.
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code. Below is the output of the program: Array shape is in the last line. Below is the code to copy: #CODE STARTS HERE----------------
import requests import numpy as np response = requests.get("https://gbfs.citibikenyc.com/gbfs/en/station_information.json") if response.status_code != 200: print("Error with website. If problem persists, contact your Facilitator!") else: print("Data download successful") datadict = response.json() print(datadict.keys()) print(datadict['data'].keys()) location_li = [] #Initialize empty list to store lon,lat tuple for data in datadict['data']['stations']: #loop through the data location_li.append((data['lon'],data['lat'])) #Store it in a list np_arr = np.array(location_li) #Convert the list to nupy array print(np_arr.shape) #Print the shape of the array #CODE ENDS HERE-----------------