Skip to content

Instantly share code, notes, and snippets.

@ritvikmath
ritvikmath / mygist
Created March 26, 2019 11:47
my gist
x=3
x+=1
print(x)
@ritvikmath
ritvikmath / call_locator_api.py
Last active April 9, 2019 02:27
Calling Starbucks Store Locator API for each zip code in LA County
#open zip code file
f = open('laZips.txt', 'r')
laZips = [z.replace('\n','') for z in f.readlines()]
allStores = []
for idx,z in enumerate(laZips):
#call request for 100 stores with current zipcode
r = requests.get('https://www.starbucks.com/store-locator?place='+z)
#if any response fails, quit immediately
@ritvikmath
ritvikmath / allStores
Created April 9, 2019 02:39
output of allStores
allStores contains a bunch of stores which each have the following format:
[Store Id, Store Location, Latitude, Longitude, City, State, Zip Code]
Here is one example:
['27689-248404', 'Carson & Wilmington', '33.831519', '-118.239858', 'Carson', 'CA', '90745']
@ritvikmath
ritvikmath / remove_duplicates.py
Last active April 9, 2019 13:35
remove duplicate stores from allStores
seenStoreIds = []
laStores = []
for store in allStores:
#if we've already seen this store id, then keep going
if store[0] in seenStoreIds:
continue
#if this is the first time seeing this store id,
#then add it to laStores and add its id to seenStoreIds
else:
@ritvikmath
ritvikmath / imports.py
Created April 9, 2019 03:02
python libraries needed for starbucks scraping
#needed to call Starbucks API
import requests
#needed to parse through text returned by Starbucks
import re
#needed to put all our nice data into a csv at the end
import pandas as pd
#needed to make fancy maps
@ritvikmath
ritvikmath / remove_outside_stores.py
Created April 9, 2019 03:19
remove starbucks locations outside la county
#open up the LA Geojson
with open('laMap.json') as f:
laArea = json.load(f)
#convert the MultiPolygon part of the LA Geojson into a shapely Polygon object for easier inclusiveness checking
laPolygon = Polygon(laArea['features'][0]['geometry']['coordinates'][0][0])
#keep store if and only if it is within the LA polygon
keepLAStores = []
for store in laStores:
@ritvikmath
ritvikmath / store_starbucks_data_to_csv.py
Last active April 9, 2019 03:53
store starbucks data into csv file
#define columns of csv
dfSbux = pd.DataFrame(columns=['id', 'strLocation', 'latitude', 'longitude', 'city', 'state', 'zip'])
#store data
for i,col in enumerate(dfSbux.columns):
dfSbux[col] = [item[i] for item in keepLAStores]
#cast the latitude and longitude as floats
dfSbux.latitude = dfSbux.latitude.apply(lambda x: float(x))
dfSbux.longitude = dfSbux.longitude.apply(lambda x: float(x))
import folium
import pandas as pd
import json
from folium import plugins
df = pd.read_csv('starbucksInLACounty.csv')
with open('laMap.geojson') as f:
laArea = json.load(f)
#initialize the map around LA County
laMap = folium.Map(location=[34.0522,-118.2437], tiles='Stamen Toner', zoom_start=9)
#add the shape of LA County to the map
folium.GeoJson(laArea).add_to(laMap)
#for each row in the Starbucks dataset, plot the corresponding latitude and longitude on the map
for i,row in df.iterrows():
folium.CircleMarker((row.latitude,row.longitude), radius=3, weight=2, color='red', fill_color='red', fill_opacity=.5).add_to(laMap)
#group the starbucks dataframe by zip code and count the number of stores in each zip code
numStoresSeries = df.groupby('zip').count().id
#initialize an empty dataframe to store this new data
numStoresByZip = pd.DataFrame()
#populate the new dataframe with a 'zipcode' column and a 'numStores' column
numStoresByZip['zipcode'] = [str(i) for i in numStoresSeries.index]
numStoresByZip['numStores'] = numStoresSeries.values
#initialize the LA County map
laMap = folium.Map(location=[34.0522,-118.2437], tiles='Stamen Toner', zoom_start=9)