This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x=3 | |
| x+=1 | |
| print(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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) |
OlderNewer