0

I am attempting to map a bunch of points from a .csv file within Jupyter Notebook using folium. Within my dataframe I have two columns named "Latitude" and "Longitude" which hold coordinate information as floats.

Here is the code I am using: **EDIT TO INCLUDE DATAFRAME CODE

import folium
from folium.plugins import FastMarkerCluster
%matplotlib inline
import pandas as pd
import re
import os
import branca.colormap as cm
import re
import numpy as np
df = pd.read_csv('D:/documents/professional/school/hunter/data/Retail_Food_Stores.csv')
print(folium.__version__)

Import .csv and modules

def createLatLong(location):
try:
    return re.findall(r'\((.*?)\)', location)
except:
    return None

def createLatLongColsX(item):
    for items in df_nyc['Lat_Long']:
        for item in items:
            try:
                return re.split(',', item)[0]
            except:
                return None

def createLatLongColsY(item):
    for items in df_nyc['Lat_Long']:
        for item in items:
            try:
                return re.split(',', item)[1]
            except:
                return None

df_nyc['Latitude'] = df_nyc['Location'].apply(createLatLongColsX)
df_nyc['Longitude'] = df_nyc['Location'].apply(createLatLongColsY)
df_nyc['Latitude'] = df_nyc['Latitude'].astype(float).fillna(0.0)
df_nyc['Longitude'] = df_nyc['Longitude'].astype(float).fillna(0.0)

Create columns holding point values

this_map = folium.Map(prefer_canvas=True)

def plotDot(point):
try:
    folium.CircleMarker(location=[point.Latitude, point.Longitude],
                        radius=2,
                        weight=10,#remove outline
                        popup = point['Entity Name'],
                        fill_color='#000000').add_to(this_map)
except:
    break

df.apply(plotDot, axis = 1, raw=True)

this_map.fit_bounds(this_map.get_bounds())

this_map

Create map and apply to dataframe rows

Fortunately, the map does display somewhat correctly in that it is appearing within the notebook. However, the point which appears is always the same regardless of how I slice the dataframe. Interestingly the name displayed in the popup when the point is clicked is always the last item name in the sliced portion of my dataframe.

So, it seems like it's pulling in the name correctly, but not populating the marker locations correctly. Using Python 2.7 within the notebook.

EDIT 2 - Est_Type_Long Latitude Longitude
0 [Multiple Operations, Store, Food Manufacturer] 40.676932 -73.969136
1 [Multiple Operations, Store, Food Manufacturer] 40.676932 -73.969136
2 [Multiple Operations, Store, Food Manufacturer] 40.676932 -73.969136
3 [Multiple Operations, Store, Bakery, Food Manu... 40.676932 -73.969136
4 [Multiple Operations, Store, Food Manufacturer] 40.676932 -73.969136

MapPeddler
  • 53
  • 9
  • I would omit `raw=True` in your apply. When raw is True you're passing numpy arrays to plotDot and you're trying to index them like a Pandas Series – Bob Haffner Oct 17 '17 at 01:38
  • Thank you for the reply. I omitted the parameter but seem to still be having the issue – MapPeddler Oct 17 '17 at 02:00
  • hmmm. That was the only glaring thing. Using an apply here is not common, but I don't see anything wrong with that approach off hand. Can you post some of your dataframe? – Bob Haffner Oct 17 '17 at 02:51
  • Gladly. Here is a [link](https://imgur.com/a/RaNU0) to images of the dataframe and my full folium code. I also am attempting to achieve the same goal using a MarkerCluster, but can't seem to get it imported correctly into the notebook from plugins – MapPeddler Oct 17 '17 at 03:07
  • I meant as text in your question. Something we can copy and paste – Bob Haffner Oct 17 '17 at 03:11
  • We're getting closer :-) . What I'm really looking for is the output of `print(df_nyc.head())` after you've created your Lat Long columns – Bob Haffner Oct 17 '17 at 03:25
  • Edit 2 @ bottom of original post. Will delete my edit comments after reply – MapPeddler Oct 17 '17 at 03:32
  • 1
    Do all your locations have the same Lat Long? – Bob Haffner Oct 17 '17 at 03:38
  • Thank you. I was overlooking the fact that the way I was pulling the Lat and Longs was incorrect, yielding only one point as you noticed. Fixing that problem cleared up my issue with the folium map and I now have a functioning inline map in my notebook. Thank you so much! Should I delete this question? – MapPeddler Oct 17 '17 at 04:06
  • Ok, cool . Yes, I would delete it – Bob Haffner Oct 17 '17 at 12:11

0 Answers0