Create a wine map with Python
In this article we will create a simple script and map of Napa Valley wineries using Python and geocoding.
One useful quality of the following example is that given a list of valid addresses, one can create all sorts of customized maps they may like, such as a map of local businesses or local industries, or extend further into more expansive national or global maps. The following example is a rudimentary example of what one can do using Python and geocoding.
I used Jupyter Notebooks in this example, but if one wants to use Jupyter Notebooks, you will need to create your own environment in order for Geopy and Folium to be installed and work. I used the Anaconda command prompt to do this. The guide linked here in installing Geopandas (not Geopy) may be helpful.
Importing and manipulating our data
A basic step in many projects like this is simply importing your data. In this example we will use Pandas, an often used Python library for data manipulation and analysis, to import our csv file of Napa Valley wineries. One can either download the spreadsheet that contains the data here, or one can visit the Napa Wine Project website, and download the spreadsheet there as well. If one decides to use the Napa Wine Project website, keep in mind that several bad addresses or non-California addresses are taken out in the Github link version, which also contains a slightly edited version of the spreadsheet as well.
import pandas as pddf = pd.read_csv(r"C:\napa_wine_full.csv", encoding= 'unicode_escape')
df
First we import the Pandas library, then use Pandas to import our data and create our dataframe. Next we will use Pandas to do some basic data manipulation.
Looking at the data you will see an Address and City column for our list of wineries. We will need to concatenate these columns and add the state of CA to the address in order for the addresses to be used with the geocode function. So next we create a dataframe column to represent this like so:
df['Map Address'] = df['Address'].astype(str) + ',' + \
df['City'] + ',' + 'CA'
df.head()Next we will drop all the P.O. Box addresses from our dataframe (you can do this before creating the “Map Address” column as well):
df_copy = df[df["Address"].str.contains("P.O. Box")==False].copy()
df = df_copy
dfIt’s important to note that it is required that anytime one filters the original dataframe it is necessary to create a copy using the copy()function of that dataframe, otherwise you will encounter errors in creating new columns, which will be necessary in creating our coordinate columns.
Geopy and geocoding
Next we will import and use the Python library Geopy, but first we need to install Geopy:
pip install geopyor if you are using Anaconda:
conda install -c conda-forge geopyGeopy is a Python library used to geocode and locate the coordinates needed of addresses, cities, countries, and landmarks used for the location points of the map. Geopy also has different geocoding services that one can use, such as Google Maps, ArcGis, and in this example the Nominatim geocoding service, which is built upon OpenStreetMap. We will use Geopy’s packages in order to prevent timeout errors, and most importantly to acquire our needed longitude and latitude coordinates.
Next we will assign our geolocating variable geolocatorto hold our Nominatim geocoding service. To avoid timeout errors we set a timeout value, otherwise you may run into a timeout error when geocoding a large numbers of addresses.
from geopy.geocoders import Nominatimgeolocator = Nominatim(user_agent="Wine Map", timeout=10)
Next we will assign our geocode variable to apply our geocode and use the RateLimiter function to delay between geocoding calls.
from geopy.extra.rate_limiter import RateLimitergeocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
We will now then create a Location column that contains our geocoded addresses. It is also important to understand that running this line of code, i.e. geocoding the addresses, may take several minutes to process.
df['Location'] = df['Map Address'].apply(geocode)
df.head()Once this is done running, you will now see our newly created geocoded “Location” column.
Get Travis Strawn’s stories in your inbox
Join Medium for free to get updates from this writer.
Next we will drop all the addresses listed as “None”, and will again use the copy() method to our dataframe to avoid issues with creating additional columns.
df_copy = df[df['Location'].notna()].copy()
df = df_copy
dfNow we create our “Point” column that lists our longitude, latitude, and altitude coordinates:
df['Point'] = df['Location'].apply(lambda loc: tuple(loc.point) if loc else None)
df.head()An then we split up the Point column into Latitude, Longitude and Altitude columns
df[['Latitude', 'Longitude', 'Altitude']] = pd.DataFrame(df['Point'].tolist(), index=df.index)
df.head()Folium and Creating our map
Next we will install and import the Python library Folium, which is used for visualizing geospatial data.
First install:
pip install foliumor if you are using Anaconda:
conda install -c conda-forge foliumNow import and create:
import foliummap = folium.Map(
location=[38.297539, -122.286865],
tiles='cartodbpositron',
zoom_start=12,
)df.apply(lambda row:folium.Marker(location=[row["Latitude"], row["Longitude"]]).add_to(map), axis=1)map
You will need to set a location for where you want the map to open to. In this example we are inputted the coordinates of Napa Valley, California. For a slightly more informative, cleaner, and a more customized example:
map = folium.Map(
location=[38.297539, -122.286865],
tiles='cartodbpositron',
zoom_start=12,
)df.apply(lambda row:folium.CircleMarker(location=[row["Latitude"], row["Longitude"]], tooltip=row["Winery"], fill=True, color = 'red', radius=1).add_to(map), axis=1)map
Folium allows many customizing features, so in the last example, we changed the marker to circle markers, added a tooltip, added a color, and set the size of the circle markers.







![4 Real Quant Trading Strategies That Actually Survive [Code Included]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*AU73i6tMRjBEOSqz63gI2w.png)
