I have a spreadsheet with Toronto real estate info - this includes the latitude and longitude of condos in the area. In fact, it even has a column with the lat & long combined. What I'd like to do is put these coordinates on a map, probably with folium (but I'm open to alternatives). From what I can tell, folium uses the following format:
map_1 = folium.Map(location=[45.372, -121.6972],
zoom_start=12,
tiles='Stamen Terrain')
folium.Marker([45.3288, -121.6625], popup='Mt. Hood Meadows').add_to(map_1)
folium.Marker([45.3311, -121.7113], popup='Timberline Lodge').add_to(map_1)
map_1
So as far as I can tell I need to do two things:
1) Generate a sufficient amount of lines with the content: folium.Marker([x, y]).add_to(map_1)
2) Fill in x and y with the lat/long values from the spreadsheet
So far I've been able to pull in the lat/long column from the csv, but that's as far as I've gotten:
import pandas as pd
import folium
df_raw = pd.read_excel('df_condo_v9_t1.xlsx', sheetname=0, header=0)
df_raw.shape
df_raw.dtypes
df_lat = df_raw['Latlng']
df_lat.head()
If you really need to look at the csv, it's at: https://github.com/vshideler/toronto-condos
Any suggestions would be appreciated!
If you're going to be working with data like this more in the future, I would strongly recommend that you read through the pandas documentation. This includes a 10 minute "getting started" guide which covers many of the common use-cases, including examples very similar to this one.
That being said, you have most of the components that you need in the code that you gave above. The two issues that I see are as follows:
If you look at the DataFrame's data types (df_raw.dtypes
in your code above), you'll see that your Latlng column is actually still a string, while Pandas has helpfully already converted the Latitude and Longitude columns to floats for you. That should tell you that it may be easier to work with those two since they'll be directly usable in positioning your marker.
You'll probably want to configure your map a bit - the defaults that you took from the example code create a terrain map centered somewhere in Oregon. Given that you're plotting properties in Toronto - neither of those seem like good options. I generally like to center my map at the mean point of my data.
A simple example to get things working could look like:
import pandas as pd
import folium
df = pd.read_csv("df_condo_v9_t1.csv")
map_center = [df["Latitude"].mean(), df["Longitude"].mean()]
map_1 = folium.Map(location=map_center, zoom_start=16)
for i, row in df[["Latitude", "Longitude"]].dropna().iterrows():
position = (row["Latitude"], row["Longitude"])
folium.Marker(position).add_to(map_1)
map_1