Question

In: Computer Science

Going through a basic tutorial on bringing in data from a URL in Python. So far,...

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:

  • import the numpy module
  • loop over all the station entries in the list of dictionaries returned by datadict['data']['stations']
  • extract the 'lon' and 'lat' entries associated with each station
  • build up a list of (lon, lat) pairs, one for each station
  • convert the list of (lon, lat) pairs to a numpy array with two columns.

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.

Solutions

Expert Solution

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-----------------

Related Solutions

Blake and his housemates started an investment club, and it has been going well. So far,...
Blake and his housemates started an investment club, and it has been going well. So far, Brett has presented a pharmaceutical company with the potential of hitting it big with a drug that is about ready to go to market after clinical trials. Peter has presented a new sushi fast-food chain that is going public in order to expand throughout the United States. Leigh is ready to present her recommendation to the group but is hesitant. Leigh’s concern is that...
With a cube from a database or data warehouse, you are bringing data "forward." Explain what...
With a cube from a database or data warehouse, you are bringing data "forward." Explain what this means and how it impacts the front-end and back-end user from a database perspective. Provide examples to illustrate your ideas. typed please
Reflect on the data mining concepts, strategies, and best practices explored so far. Consider data mining...
Reflect on the data mining concepts, strategies, and best practices explored so far. Consider data mining from both a global perspective in the management of big data and the impact of data mining on individual organizations.
Greetings, I worked through part A and assuming I got it all correct so far. I'm...
Greetings, I worked through part A and assuming I got it all correct so far. I'm not sure where to go or how to find the breakeven point from the information found & given in order to complete B and C. 1. You are currently in the 7th year of a 25-year fixed rate, fully amortized mortgage with monthly payments. You originally borrowed $150,000 with a 9.50% annual interest rate. Interest rates are expected to be lower, so you are...
From what you've learnt so far, write a report on the agile development methodology from the...
From what you've learnt so far, write a report on the agile development methodology from the point of view of a programmer trying to argue the case for management of Academic City College to adopt a specific methodology for internal system development and contracted (procured) system development. You can assume the organisation has no experience in agile development processes at all so you will need to provide detailed arguments (this is not true but for the purposes of the assignment...
Build all classification & regression models you learned so far for wine quality prediction. No data...
Build all classification & regression models you learned so far for wine quality prediction. No data partition is needed. Please don’t modify the original categories of wine quality. 2: Which approach will you recommend, classification or regression models? Explain the reasons, why?
So far there is no multilateral level investment agreement (MIA), despite attempts from OECD, UN, and...
So far there is no multilateral level investment agreement (MIA), despite attempts from OECD, UN, and other international institutions. What do you think are the key reasons for the failure of MIA?   Explain the major reasons (read Young and Tavares, 2004).
Below are 6 important terms from the course so far.  In your own words and in complete...
Below are 6 important terms from the course so far.  In your own words and in complete sentences, please identify four of the terms and explain each term's significance to the story of American history. Be as specific as possible. The Pueblo Rebellion The Great Awakening The Middle Passage The Spanish Mission System James Oglethorpe The Stamp Act of 1765
Question 6 (7 marks) (This question is from the Week 11 Tutorial) The following data available...
Question 6 (This question is from the Week 11 Tutorial) The following data available for ABC company. Account Beginning balance Ending Balance Use/source of cash Accounts payable 20,300 24,400 Inventory 60,600 67,200 Long term debts 127,500 125,800 Common stock 200,400 215,900 Required: a) Calculate and identify the source of cash or the use of cash for each account change by filling into the column next to the ending balance. b) Assume that beginning balance of accounts receivable is $23 400...
Based on what you know about metabolism from the lectures you have had so far in...
Based on what you know about metabolism from the lectures you have had so far in this course, which two of the following would you expect to be the MOST LIKELY to have signs and symptoms that are nearly identical to each other? TPID (triose phosphate isomerase deficiency, an inability to make triose phosphate isomerase) Arsenate poisoning (arsenate can substitute for phosphate, but bonds between AsO43- and carbohydrates instantly hydrolyze whereas acyl phosphate bonds do not) Beriberi, a dietary deficiency...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT