我正在尝试创建一张基于多伦多社区负担不起的住房价格的克洛普斯地图。我在网上找到了一个数据集,我在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
发布于 2020-05-19 10:01:13
您尝试读取的数据是JSON文件,而不是CSV文件。
import json
with open('toronto.geojson.txt', 'rb') as f:
data = json.load(f)https://stackoverflow.com/questions/61880207
复制相似问题