首页 > 解决方案 > Folium Map 内存不足

问题描述

我一直在使用随附的代码来构建一个 HTML 文件,该文件包含一个带有来自数据框的许多标记的folium 地图。直到今天我一直在完美地使用它。现在它挂了很长时间,并在保存 html 文件时给我一个内存错误。当我注释掉 HTML 文件行时,代码会在一段时间后运行。

为什么我现在会收到此错误?

我的代码可以提高效率吗?

import pandas as pd
import folium
import folium.plugins as plugins
import folium.map as fm


df = pd.read_excel('Stores_lat_long.xlsx')
df = df.dropna(subset=['Store Latitude'])
df = df.dropna(subset=['Store Longitude'])


sf = pd.read_excel('Stores_lat_long.xlsx', 1, index_col=0)

#oh / (sales/6)

sf['WOS'] = sf['Repl Inv Units'] / (sf['Sales Units'] / 6)

df = pd.merge(df, sf)

print(df)


#defining inital Map Object
m = folium.Map(
    location=[36.166340, -86.779068], 
    zoom_start=4)

folium.TileLayer('openstreetmap').add_to(m)
folium.TileLayer('Stamen Terrain').add_to(m)
folium.TileLayer('Stamen Toner').add_to(m)



#Define Features

fg = folium.FeatureGroup(name='Hardwood Only Stores Code 01', show=False)
m.add_child(fg)

gg = folium.FeatureGroup(name='Softwood Only Stores Code 01', show=False)
m.add_child(gg)

fgcl = folium.FeatureGroup(name='Hardwood Clustered', show=False)
fgcl_markers = plugins.MarkerCluster().add_to(fgcl)
m.add_child(fgcl)


ggcl = folium.FeatureGroup(name='Softwood Clustered', show=False)
ggcl_markers = plugins.MarkerCluster().add_to(ggcl)
m.add_child(ggcl)

# tie to top right layer control

folium.LayerControl('topright', collapsed=False).add_to(m)

#Hardwood Pellets un-clusterd

for i in range(0, len(df)):
    if df['Std Code'].iloc[i] != 1: continue
    if df['Article No'].iloc[i] != 3195163: continue

    sku = df['Article No'].iloc[i]
    store = df['Store No'].iloc[i]
    code = df['Std Code'].iloc[i]
    units = df['Repl Inv Units'].iloc[i]
    oo = df['On Order Units'].iloc[i]
    region = df['Region Full'].iloc[i]
    dm = df['Dist DM Name'].iloc[i]
    wos = df['WOS'].iloc[i]


    test = folium.Html('''

        <body>
            Store Number: <strong> {store} </strong> <br>
            Pellet SKU: <strong> {sku} </strong> <br>
            Store Coded: <strong> {code} </strong> <br>
            On-Hand Units: <strong> {units} </strong> <br>
            On-Order Units: <strong> {oo} </strong> <br>
            Region: <strong> {region} </strong> <br>
            DM: <strong> {dm} </strong> <br>
            WOS: <strong> {wos} </strong> <br>

        </body>'''.format(sku=sku, store=store, code=code, units=units, region=region, dm=dm, oo=oo,wos=wos), script=True)

    popup = folium.Popup(test, max_width=2650)
    folium.Marker(
                [df['Store Latitude'].iloc[i], df['Store Longitude'].iloc[i]], 
                popup=popup,
                tooltip= 'Store:' + str(df['Store No'].iloc[i]), 
                icon=folium.Icon(color='darkred',icon="home", prefix='fa')).add_to(fg)
    continue


#Softwood Pellets Unclustered
for i in range(0, len(df)):
    if df['Article No'].iloc[i] != 1115622: continue
    if df['Std Code'].iloc[i] != 1: continue

    sku = df['Article No'].iloc[i]
    store = df['Store No'].iloc[i]
    code = df['Std Code'].iloc[i]
    units = df['Repl Inv Units'].iloc[i]
    oo = df['On Order Units'].iloc[i]
    region = df['Region Full'].iloc[i]
    dm = df['Dist DM Name'].iloc[i]
    wos = df['WOS'].iloc[i]

    test = folium.Html('''

        <body>
            Store Number: <strong> {store} </strong> <br>
            Pellet SKU: <strong> {sku} </strong> <br>
            Store Coded: <strong> {code} </strong> <br>
            On-Hand Units: <strong> {units} </strong> <br>
            On-Order Units: <strong> {oo} </strong> <br>
            Region: <strong> {region} </strong> <br>
            DM: <strong> {dm} </strong> <br>
            WOS: <strong> {wos} </strong> <br>

        </body>'''.format(sku=sku, store=store, code=code, units=units, region=region, dm=dm, oo=oo, wos=wos), script=True)

    popup = folium.Popup(test, max_width=2650)
    folium.Marker(
                [df['Store Latitude'].iloc[i], df['Store Longitude'].iloc[i]], 
                popup=popup,
                tooltip= 'Store:' + str(df['Store No'].iloc[i]), 
                icon=folium.Icon(color='blue',icon="building", prefix='fa')).add_to(gg)
    continue


#Hardwood Clustered
for i in range(0, len(df)):
    if df['Std Code'].iloc[i] != 1: continue
    if df['Article No'].iloc[i] != 3195163: continue


    sku = df['Article No'].iloc[i]
    store = df['Store No'].iloc[i]
    code = df['Std Code'].iloc[i]
    units = df['Repl Inv Units'].iloc[i]
    oo = df['On Order Units'].iloc[i]
    region = df['Region Full'].iloc[i]
    dm = df['Dist DM Name'].iloc[i]
    wos = df['WOS'].iloc[i]


    test = folium.Html('''

        <body>
            Store Number: <strong> {store} </strong> <br>
            Pellet SKU: <strong> {sku} </strong> <br>
            Store Coded: <strong> {code} </strong> <br>
            On-Hand Units: <strong> {units} </strong> <br>
            On-Order Units: <strong> {oo} </strong> <br>
            Region: <strong> {region} </strong> <br>
            DM: <strong> {dm} </strong> <br>
            WOS: <strong> {wos} </strong> <br>

        </body>'''.format(sku=sku, store=store, code=code, units=units, region=region, dm=dm, oo=oo, wos=wos), script=True)

    popup = folium.Popup(test, max_width=2650)
    folium.Marker(
                [df['Store Latitude'].iloc[i], df['Store Longitude'].iloc[i]], 
                popup=popup,
                tooltip= 'Store:' + str(df['Store No'].iloc[i]), 
                icon=folium.Icon(color='darkred',icon="home", prefix='fa')).add_to(fgcl_markers)
    continue



#Softwood Clustered
for i in range(0, len(df)):
    if df['Article No'].iloc[i] != 1115622: continue
    if df['Std Code'].iloc[i] != 1: continue

    sku = df['Article No'].iloc[i]
    store = df['Store No'].iloc[i]
    code = df['Std Code'].iloc[i]
    units = df['Repl Inv Units'].iloc[i]
    oo = df['On Order Units'].iloc[i]
    region = df['Region Full'].iloc[i]
    dm = df['Dist DM Name'].iloc[i]
    wos = df['WOS'].iloc[i]

    
    test = folium.Html('''

        <body>
            Store Number: <strong> {store} </strong> <br>
            Pellet SKU: <strong> {sku} </strong> <br>
            Store Coded: <strong> {code} </strong> <br>
            On-Hand Units: <strong> {units} </strong> <br>
            On-Order Units: <strong> {oo} </strong> <br>
            Region: <strong> {region} </strong> <br>
            DM: <strong> {dm} </strong> <br>
            WOS: <strong> {wos} </strong> <br>

        </body>'''.format(sku=sku, store=store, code=code, units=units, region=region, dm=dm, oo=oo, wos=wos), script=True)

    

    popup = folium.Popup(test, max_width=2650)
    folium.Marker(
                [df['Store Latitude'].iloc[i], df['Store Longitude'].iloc[i]], 
                popup=popup,
                tooltip= 'Store:' + str(df['Store No'].iloc[i]), 
                icon=folium.Icon(color='blue',icon="building", prefix='fa')).add_to(ggcl_markers)
    continue



m.save(outfile='pellet_map2.html')
df.to_excel('ThisOne.xlsx')

错误信息

标签: pythonpandasmemoryramfolium

解决方案


我弄清楚了这个问题。我错误地合并了两个数据帧,结果 df 是 55,000+ 行。我的地图试图在 chrome 浏览器中绘制出所有这些点……这太过分了。在冻结您的浏览器之前,Folium 地图的最高点约为 3000 点。


推荐阅读