Generating a custom map from Pandas’ DataFrame

Bünyamin Sarı
Aug 2 · 2 min read

Are you ready for creating some custom, open source map for your project? Well, I wasn’t either, however we will start with one tiny step. Let’s get started.

Photo by Martin Sanchez on Unsplash

Let’s import the important python libraries that we are going to use in this project. The libraries are “folium”, “pandas” and “geopy”.

Here is our first code snippet. We will be using the API of ArcGIS, and we will initialize it as a function and assign it to the nom as nominator.

import folium
import pandas
from geopy import ArcGIS
nom = ArcGIS()

Our main aim in this projects is to get the user input, as we will collect the proper address from the users, then using the pandas library, we will generate a DataFrame, and concatenate the collected data as full address. Here in this stage, geopy will reach our help and it will abstract the coordinates from the address using geocode function.

To collect the proper address, we use this snippet:

print(“Please enter your address accordingly!”)
address = input(“Address: “)
city = input(“City: “)
state = input(“State: “)
zip = input(“Zip: “)

We will collect, “Address” as “25 N Michigan Ave” and “City” as Chicago, “State” as in IL and lastly, “zip” as 60602.

df = pandas.DataFrame([[address,city,state,zip]],columns=[“address”,”city”,”state”,”zip”])df[“Full Address”] = df[“address”]+”,”+df[“city”]+”,”+df[“state”]+ “,”+ str(df[“zip”])

After collecting the required address, we will use pandas library and create a DataFrame, in this data frame the data will be saved as lists. In order to use geocode we need to collected data to be formed as a whole. So we concatenate the columns and will make another column in the DataFrame.

df[“Coordinates”] = df[“Full Address”].apply(nom.geocode)

Then, with the help of the nom.geocode function, we will extract the latitude and longitude and saved them in another column in the DataFrame. Now, the “Coordinates” column in the DataFrame consists the latitude and longitude information.

latitude = df[“Latitude”] = df[“Coordinates”].apply(lambda la : la.latitude if la != None else None)longitude = df[“Longitude”] = df[“Coordinates”].apply(lambda lo : lo.longitude if lo != None else None)

In the above snippet, we get the exact coordinates to use them as an input in the folium.Map() function, to create a map. Let’s check the below snippet.In this code, we make a variable called map and assign it to a function called folium.Map() and it has an argument “location” which takes a list, and it consist of latitude and longitude.

map = folium.Map(location=[latitude,longitude])
map.save(“map1.html”)

In conclusion, we will use save() function to create a new map called “map1.html” with folium python library.

Here you can access to whole code: https://github.com/bunyaminsari/map

Don’t forget to check this link out; https://benedtech.medium.com/membership