pandas - 如何显示叶特征组中的数据总和
问题描述
我想做的事:
我想在地图中显示所选数据的总和。因此,如果我检查 data2、data4 和 data5,它会在标记中显示这些数据帧的总和。
它现在的作用:
现在,当我检查 data2、data4 和 data5 时,它只会显示每张地图顶部的底部,所以在这种情况下,它会显示 data5。
这是我的代码:
#Reading data
df = pd.read_csv('C:/EzterLAPTOP/Ezter/source/Data/taulu2.csv', encoding='utf-8')
dictionary={'ä':'a','ö':'o','Ä':'A','å':'a'}
df.replace(dictionary, regex=True, inplace=True)
#Reading geojson
kunnat_geo = r'C:/EzterLAPTOP/Ezter/kuntarajat.geojson'
with open(kunnat_geo, encoding='utf-8') as kunnat_file:
kunnat_json = json.load(kunnat_file)
#Cleaning data
df = df.rename(columns={'ALUE':'Kunta'})
df.drop(df.loc[df['lat']=='0.0'].index, inplace=True)
df.dropna(inplace=True)
df = df[df.lat != 0]
#Creating map
map = folium.Map(location=[65,26], zoom_start=4, tiles='openstreetmap')
#Making a function that creates a choropleth map
def choromapping(name, col1):
choropleth = folium.Choropleth(
geo_data=kunnat_geo,
name=name,
data=df,
columns=['Kunta',col1],
key_on='feature.properties.Name',
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.2,
legend_name=name,
show=False
).add_to(map)
choromarker = MarkerCluster().add_to(choropleth)
#Adding markers
for i in range(0,len(df)):
folium.Marker(
[df.iloc[i]['lat'],
df.iloc[i]['lng']],
popup=(df.iloc[i][col1],
df.iloc[i]['Kunta']),
tooltip=df.iloc[i]['Kunta']
).add_to(choromarker)
#Generating maps
choromapping('data1','MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018')
choromapping('data2','KEHITYSVAMMALAITOKSEN_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data3','VANHAINKOTIEN_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data4','KOTIHOIDON_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data5','TEHOSTETUN_HOIDON_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data6','PALVELUASUMISEN_ASIAKKAAT_YHTEENSÄ_2018')
LayerControl().add_to(map)
map
解决方案
Changed a few things
- using
map
as a variable name.map
is a key function in core Python - there's no need for so many separate hits on dataframe with
iloc
just loop over data you need - excluded points where value is zero. I assume you don't really want to put these into the layer
- changed popup to be built from an f-string instead of passing a
tuple
- you note you want
sum
. I didn't see your data could be aggregateddf.loc[:,["lat","lng","Kunta",'MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018']].groupby(["lat","lng","Kunta"]).sum()
still returns 311 rows
import folium.plugins
import pandas as pd
import json
#Reading data
df = pd.read_csv('taulu2.csv', encoding='utf-8')
dictionary={'ä':'a','ö':'o','Ä':'A','å':'a'}
df.replace(dictionary, regex=True, inplace=True)
#Reading geojson
kunnat_geo = r'kuntarajat.geojson'
with open(kunnat_geo, encoding='utf-8') as kunnat_file:
kunnat_json = json.load(kunnat_file)
#Cleaning data
df = df.rename(columns={'ALUE':'Kunta'})
df.drop(df.loc[df['lat']==0.0].index, inplace=True)
#Creating map
m = folium.Map(location=[65,26], zoom_start=4, tiles='openstreetmap')
#Making a function that creates a choropleth map
def choromapping(name, col1):
choropleth = folium.Choropleth(
geo_data=kunnat_geo,
name=name,
data=df,
columns=['Kunta',col1],
key_on='feature.properties.Name',
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.2,
legend_name=name,
show=False
).add_to(m)
choromarker = folium.plugins.marker_cluster.MarkerCluster().add_to(choropleth)
#Adding markers
for r in df.loc[df[col1].ne(0),["lat","lng","Kunta",col1]].to_dict(orient="records"):
folium.Marker(
[r['lat'],
r['lng']],
popup=f"({r[col1]},{r['Kunta']})",
tooltip=r['Kunta']
).add_to(choromarker)
#Generating maps
choromapping('data1','MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018')
choromapping('data2','KEHITYSVAMMALAITOKSEN_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data3','VANHAINKOTIEN_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data4','KOTIHOIDON_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data5','TEHOSTETUN_HOIDON_ASIAKKAAT_YHTEENSÄ_2018')
choromapping('data6','PALVELUASUMISEN_ASIAKKAAT_YHTEENSÄ_2018')
folium.map.LayerControl().add_to(m)
m
推荐阅读
- here-api - HERE Places (Search) API - 类别系统文档和 API 响应之间的差异
- javascript - 如何将图像和数组与本机反应一起发布到后端?
- angular - 了解使用 Angular 制作的 Web 应用程序的性能
- node.js - 在邮递员“null”中发出删除请求后得到这样的响应?
- python - BLE 数据在 Python 中转换为 Bytearray
- reactjs - 即使没有剩余空间,Froala 工具栏弹出窗口也会出现在底部
- android - 在存储库类中使用 dagger-hilt
- scheduled-tasks - 如何创建一个 Teamcity 构建触发器,该触发器将在作业 A 完成后每周运行一次作业 B,其中 A 每天运行
- autodesk-forge - Autodesk Forge 查看器:直接加载 SVF?
- twitter-bootstrap-3 - 如何从 Fullcalendar 中删除 fc-time?