Skip to content

Instantly share code, notes, and snippets.

@ritvikmath
Created April 19, 2019 12:10
Show Gist options
  • Select an option

  • Save ritvikmath/372c8fa8a4697e9238b7fe4538842df7 to your computer and use it in GitHub Desktop.

Select an option

Save ritvikmath/372c8fa8a4697e9238b7fe4538842df7 to your computer and use it in GitHub Desktop.
#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)
#draw the choropleth map. These are the key components:
#--geo_path: the geojson which you want to draw on the map [in our case it is the zipcodes in LA County]
#--data: the pandas dataframe which contains the zipcode information
# AND the values of the variable you want to plot on the choropleth
#--columns: the columns from the dataframe that you want to use
#[this should include a geospatial column [zipcode] and a variable [numStores]
#--key_on: the common key between one of your columns and an attribute in the geojson.
#This is how python knows which dataframe row matches up to which zipcode in the geojson
laMap.choropleth(geo_path='laZips.geojson', data=numStoresByZip, columns=['zipcode', 'numStores'], \
key_on='feature.properties.zipcode', fill_color='YlGn', fill_opacity=1)
laMap.save('laChoropleth.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment