More Than a Feeling — it’s a Rating

Boston

’Tis the season for adventure, specifically a ladies trip. A time to make somewhat post pandemic memories. My friends are all vaccinated so we’ve decided to visit our friend in Boston.

“She said I think I’ll go to Boston” -Augustana

Conveniently the weekend I’m traveling to Boston with the girls, happens to be the same week my brother is moving to Boston. As a result, I took the liberty of doing some Yelp research on the best spots in Boston.

Photo by Lance Anderson on Unsplash
import pandas as pd
import numpy as np
import requests
import json
#from yelp.client import Client
import matplotlib.pyplot as plt

To get some data from yelp

url = 'https://api.yelp.com/v3'
response = requests.get(url)

Check the status code

# check the status code
response.status_code

Insert your API key to pull in the first 50 bagel places in Boston

MY_API_KEY = "insert_your_key_here"term = 'bagels'
location = 'Boston'
SEARCH_LIMIT = 50
url = 'https://api.yelp.com/v3/businesses/search'headers = {
'Authorization': 'Bearer {}'.format(MY_API_KEY),
}
url_params = {
'term': term.replace(' ', '+'),
'location': location.replace(' ', '+'),
'limit': SEARCH_LIMIT
}
response_bagels = requests.get(url, headers=headers, params=url_params)

Check response

print(response_bagels)

Turn the text into a JSON file

bagels = json.loads(response_bagels.text)
print(type(bagels)) #spoiler - jsons are dictionaries

Use the following code to find the keys in the JSON dictionary

for key in bagels.keys():
print(key)

Using the values of the business key to create a dataframe

#making the bagels dictionary a dataframe
bagels_df = pd.DataFrame.from_dict(bagels['businesses'])

Find out the columns

bagels_df.columns

Reduce amount of columns

df = bagels_df.loc[:,['name', 'rating', 'review_count', 'price', 'location']]

Sort the dataframe from highest rated bagels to lowest rated bagels

df = df.sort_values('rating', ascending = False)

Make graph

fig, ax = plt.subplots(figsize = (15,7.5))
x = df['name'][:25]
y = df['rating'][:25]
ax.set_xlabel('Name', fontsize = 17)
ax.set_ylabel('Rating', fontsize = 17)
ax.set_title('Bagel Places Boston', fontsize = 24)
plt.xticks(rotation=80, fontsize = 15)
ax.bar(x,y, color = 'dodgerblue')
plt.savefig('../data/Bagels_namerating')

And repeat the process with other terms. Instead of the string bagel we can use the string coffee.

Photo by Katarzyna Grabowska on Unsplash | Charles River

Aspiring Data Scientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store