Ty Swenson
Ty Swenson

Reputation: 59

folium markers/popups from data with duplicated lat/long

I am attempting make a simple folium map that will plot markers based on given lat/long as well as popups with data about the point.

an example dataframe is like the below:

d = {'Location code': [132, 132, 132, 556, 556, 430],
     'Product code': [1072567, 1309006, 1400940, 7776654, 7776654, 7551230],
     'Days of Supply': [50, 22, 24, 77, 100, 32],
    'Latitude': [42.97046, 42.97046, 42.97046, 41.470183, 41.470183, 38.864575],
    'Longitude': [80, 80 , 80, 72, 72, 60]}
map_df = pd.DataFrame(data=d)

The issue is that I have repeat rows for locations from my data source, therefore when I iterate and plot the folium markers on a map, I am only able to access the other information (product codes, supply, etc) from the last row.

for grp_name, df_grp in map_df.groupby(['Location code', 'Product code']):
    for row in df_grp.itertuples():
        store = row[0]

        test = folium.Html('''
            Store Number: {store} <br>
            '''.format(store=store), script=True)
        popup = folium.Popup(test, max_width=2550)
        color = color_selector(dos)
        folium.Marker(location=[row.Latitude, row.Longitude], popup=popup,icon=folium.Icon(color=color, icon='home', prefix='fa')).add_to(m)

Any help would be greatly appreciated. I have considered flattening my dataframe to only have locations listed once, but I was unsure how to handle data in those duplicated rows in order to access their values when making a popup later.

Upvotes: 0

Views: 877

Answers (1)

r-beginners
r-beginners

Reputation: 35115

Group the original data frame and take the sum. With that aggregated data frame, extract the data frame by product code and add the product code and quantity to the list with the resulting data frame. The resulting list is divided by <br> as a popup.

map_df_gr = map_df.groupby(['Location code', 'Product code', 'Latitude', 'Longitude'])['Days of Supply'].sum().to_frame('Days_of_supply_sum')
map_df_gr.reset_index(inplace=True)

map_df_gr
    Location code   Product code    Latitude    Longitude   Days_of_supply_sum
0   132     1072567     42.970460   80  50
1   132     1309006     42.970460   80  22
2   132     1400940     42.970460   80  24
3   430     7551230     38.864575   60  32
4   556     7776654     41.470183   72  177

m = folium.Map(location=[map_df_gr['Latitude'].mean(),map_df_gr['Longitude'].mean()], zoom_start=5)

for code in map_df_gr['Location code'].unique():
    product_sum = []
    dff = map_df_gr[map_df_gr['Location code'] == code]
    for row in dff.itertuples():
        product_sum.append('Productcode: {},Days of supply: {}'.format(row[2],row[5]))
    popup = folium.Popup('<br>'.join(product_sum), max_width=500)
    folium.Marker(location=[row.Latitude, row.Longitude], 
                  popup=popup,
                  icon=folium.Icon(color='green', icon='home', prefix='fa')).add_to(m)
m

enter image description here

Upvotes: 1

Related Questions