Folium:类型为“ DataFrame”的TypeError对象不是JSON可序列化的

时间:2018-07-15 06:09:08

标签: python pandas folium

我正在使用一个简单的.csv,如下所示:

Latitude,Longitude,Count,Zero
49.978728,11.949353,100,0
49.978728,11.950353,2,0

然后将其转换为熊猫数据框:

将熊猫作为pd导入 进口叶 将numpy导入为np

 data = pd.read_csv("farmers_location.csv")

 #extract value from data
 count = data.loc[:, ["Count"]]
 latitude = data.loc[:, ["Latitude"]]
 longitude = data.loc[:, ["Longitude"]]

 #df_counters = pd.DataFrame(count,latitude,longitude)
 df_counters = count.join(latitude).join(longitude)
 df_counters.head()

 # forcely convert the numpy.int64 into string 
 df_counters['Count'] = df_counters['Count'].astype(str)


 locations = df_counters[['Latitude', 'Longitude']]
 locationlist = locations.values.tolist()
 len(locationlist)
 locationlist[1]

然后,我构造一张地图:

map = folium.Map(location=[49.978, 11.950], zoom_start=20)
for point in range(0, len(locationlist)):
     folium.Marker(locationlist[point], popup=df_counters['Count'] 
     [point]).add_to(map)

这很好,但是,如果我将最后三行替换为:

 map = folium.Map(location=[49.978, 11.950], zoom_start=12)
 for point in range(0, len(locationlist)):
      radius = count/20
      color1 = "#0A8A9F"
      color2= "#E37222"
      color= np.where(count>99, color1, color2)
      folium.CircleMarker(locationlist[point],
                          radius=radius,
                          color=color,
                          fill=True).add_to(map)

我最终遇到以下错误:

  

TypeError:“ DataFrame”类型的对象不可JSON序列化

编辑:自从我解决了这个问题,这里有两种解决方法:

您可以通过str(color)和str(radius)将颜色和半径转换为字符串

0 个答案:

没有答案