我正在尝试根据多伦多附近地区负担不起的房屋价格创建一个拟人地图。我在网上找到了一个数据集,将其读入一个csv中,然后对数据进行条件化以创建适当的列,然后尝试创建我的地图。
我的数据和条件代码如下:
df = pd.read_csv('torontodata.csv')
df = df.transpose()
df.columns = df.iloc[4]
df.drop(['_id' , 'Category', 'Topic', 'Data Source', 'Characteristic'], axis=0, inplace=True)
df = df.reset_index()
df = pd.DataFrame(df[['index', 'Rate of unaffordable housing']])
df.drop(df.loc[df['index']=='City of Toronto'].index, inplace=True)
df['Rate of unaffordable housing'] = df['Rate of unaffordable housing'].astype(float)
我相当有信心这样做正确,因为它完全可以返回:dataframe。我认为错误一定是在choropleth映射的'key_on'参数中,但是我不知道我做错了什么。我检查了原始的geojson文件,似乎我有正确的路径。
!wget --quiet https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson
print('GeoJSON file downloaded!')
toronto_geo = r'toronto_crs84.geojson' # geojson file
address = 'Toronto, ON'
geolocator = Nominatim(user_agent="foursquare_agent")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print(latitude, longitude)
toronto_map = folium.Map(location=[latitude, longitude], zoom_start=13) # generate map centred around Toronto
threshold_scale = np.linspace(df['Rate of unaffordable housing'].min(),
df['Rate of unaffordable housing'].max(),
6, dtype=int)
threshold_scale = threshold_scale.tolist() # change the numpy array to a list
threshold_scale[-1] = threshold_scale[-1] + 1 # make sure that the last value of the list is greater than the maximum immigration
# display map
toronto_map.choropleth(
geo_data=toronto_geo,
data=df,
columns=['index', 'Rate of unaffordable housing'],
key_on='feature.properties.name',
threshold_scale=threshold_scale,
fill_color='YlOrRd',
fill_opacity=0.3,
line_opacity=0.2,
legend_name='Affordable housing in Canada'
)
toronto_map
这不会返回错误,但是我的地图全是一种颜色值。我整天都在解决这个问题。我什至尝试使用其他json文件,但遇到了同样的问题。任何帮助,将不胜感激。
https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson
您尝试读取的数据是JSON文件,而不是CSV文件。
import json
with open('toronto.geojson.txt', 'rb') as f:
data = json.load(f)