LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Streamlined Data Ingestion with pandas

Kurs anzeigen

Anleitung zur Übung

  • Create a dictionary, parameters, with the term and location parameters set to search for "cafe"s in "NYC".
  • Query the Yelp API (api_url) with requests's get() function and the headers and params keyword arguments set. Save the result as response.
  • Extract the JSON data from response with the appropriate method. Save the result as data.
  • Load the "businesses" values in data to the dataframe cafes and print the head.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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())
Code bearbeiten und ausführen