Set API parameters
Formatting parameters to get the data you need is an integral part of working with APIs. These parameters can be passed to the get()
function's params
keyword argument as a dictionary.
The Yelp API requires the location
parameter be set. It also lets users supply a term
to search for. You'll use these parameters to get data about cafes in NYC, then process the result to create a dataframe.
pandas
(as pd
) and requests
have been loaded. The API endpoint is stored in the variable api_url
. Authorization data is stored in the dictionary headers
.
This exercise is part of the course
Streamlined Data Ingestion with pandas
Exercise instructions
- Create a dictionary,
parameters
, with theterm
andlocation
parameters set to search for"cafe"
s in"NYC"
. - Query the Yelp API (
api_url
) withrequests
'sget()
function and theheaders
andparams
keyword arguments set. Save the result asresponse
. - Extract the JSON data from
response
with the appropriate method. Save the result asdata
. - Load the
"businesses"
values indata
to the dataframecafes
and print the head.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create dictionary to query API for cafes in NYC
parameters = {____,
____}
# Query the Yelp API with headers and params set
response = ____(____,
____,
____)
# Extract JSON data from response
data = ____
# Load "businesses" values to a dataframe and print head
cafes = ____(____)
print(cafes.head())