Going through a basic tutorial on bringing in data froma URL in Python. So far, I've imported Citi Bike station data andconverted the JSON file to a Python dictionary nameddatadict...
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 yourFacilitator!\")
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 cellbelow, write and evaluate code to extract the latitude andlongitude of every bike station, and store the result in a variablenamed coordinates. Store the data in a numpy array of shape (N,2),where N is the total number of stations, and 2 reflects the numberof columns — store all the longitudes in the first column and allthe latitudes in the second column.
Carry out this dataextraction however you see fit. The basic steps you will need tocarry out are:
- import the numpy module
- loop over all the station entries in the list of dictionariesreturned by datadict['data']['stations']
- extract the 'lon' and 'lat' entries associated with eachstation
- build up a list of (lon, lat) pairs, one for each station
- convert the list of (lon, lat) pairs to a numpy array with twocolumns.
After importing numpy,the remaining set of steps above can be done in one line, using alist comprehension to extract the desired pair of fields from eachstation entry and then converting the list to a numpy array usingthe np.array function. But if you'd rather break out each stepseparately within a for loop, that would work too. When you arefinished, you should have a numpy array with shape approximatelyequal to (1000, 2), since there should be approximately 1000stations in the full array of coordinates (although that number canchange over time as new stations are added or removed). Print theshape of the array to determine how many stations are included inthe dataset that you downloaded.