首页 > 解决方案 > 叶图返回“TypeError:int64 类型的对象不是 JSON 可序列化的”

问题描述

在我下面的脚本中,Folium 图返回“TypeError:int64 类型的对象不是 JSON 可序列化的”。脚本的最后一行似乎有问题,因为当我注释它时,它会运行,但是当我取消注释它时,我得到 TypeError。我需要将文件绘制为 html 或 jpg。

import pandas as pd 
import folium
from folium.plugins import HeatMap
from folium.plugins import MarkerCluster  
for_map = pd.read_csv('datasets/sum_loads.csv')
max_amount = float(for_map['total_offs_sum'].max())
hmap = folium.Map(location=[51.5, -0.1], zoom_start=7)
hm_wide = HeatMap( list(zip(for_map.latitude.values, for_map.longitude.values,     for_map.total_offs_sum.values)),
                   min_opacity=0.2,
                   max_val=max_amount,
                   radius=17, blur=12, 
                   max_zoom=1, 
                 )
hmap.add_child(hm_wide)
locations = list(zip(for_map.latitude, for_map.longitude))
cluster = MarkerCluster(locations)
hmap.add_child(cluster)
hm_wide.save('output_plot.html')

标签: pythonhtmlfolium

解决方案


根据 Christian 的建议,您的 for_map.total_offs_sum.values 很可能是整数。只需尝试将其转换为浮动即可。例如

hm_wide = HeatMap(list(zip(for_map.latitude.values, for_map.longitude.values,for_map.total_offs_sum.values.astype(float))),
                       min_opacity=0.2,
                       max_val=max_amount,
                       radius=17, blur=12, 
                       max_zoom=1, 
                     )    

推荐阅读