我正在尝试使用folium创建choropleth地图,这是我的代码:
import pandas as pd
import numpy as np
df3 = pd.read_csv(r'crime in SF.csv')
count = ['Count']
df4 = df3.groupby('Neighborhood')[count].sum()
df4.reset_index(inplace = True)
df4['Count'] = df4['Count'].astype(int)
data = {"Neighborhood": df4["Neighborhood"], 'Count': df4['Count']}
df5= pd.DataFrame(data)
在此处输入图像描述
import json
sf= json.load(open('san-francisco.geojson', 'r'))
sf= r'san-francisco.json'
import folium
world_map=folium.Map([37.77, -122.42], zoom_start=12, tiles='Mapbox Bright')
threshold_scale = np.linspace(df5['Count'].min(),
df5['Count'].max(),
6, dtype= int)
threshold_scale = threshold_scale.tolist()
threshold_scale[-1]= threshold_scale[-1] + 1
folium.Choropleth(geo_data= sf,
name ='choropleth',
data= df5,
columns= ['Neighborhood','Count'],
key_on='feature.properties.OBJECTID',
color='YlOrRd',
highlight=True,
fill_opacity = 0.7,
line_opacity=0.2,
legend_name='Crime Rate in San Fancisco',
threshold_scale=threshold_scale,
reset=True).add_to(world_map)
folium.LayerControl().add_to(world_map)
display(world_map)
在此处输入图像描述
它总是返回一个关于箱中某些东西的错误(见图2(,并说一些关于直方图的内容,尽管我还没有写任何关于直方图的代码
变量threshold_scale
中的bin有问题。您可以打印此变量的内容以了解问题。您指定要将bin作为int
数字,我认为numpy将其存储在32位(或更小(的整数中,因此您可以使用的最大数字是2147483647,并且您在df5
中的值高于该数字。为了解决您的问题,您可以使用int64
:
threshold_scale = np.linspace(df5['Count'].min(),
df5['Count'].max(),
6, dtype= np.int64)