首页 > 解决方案 > 如何显示叶特征组中的数据总和

问题描述

我想做的事:

我想在地图中显示所选数据的总和。因此,如果我检查 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

我的数据 我的geojson

标签: pandasfoliumchoropleth

解决方案


Changed a few things

  1. using map as a variable name. map is a key function in core Python
  2. there's no need for so many separate hits on dataframe with iloc just loop over data you need
  3. excluded points where value is zero. I assume you don't really want to put these into the layer
  4. changed popup to be built from an f-string instead of passing a tuple
  5. you note you want sum. I didn't see your data could be aggregated df.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

enter image description here


推荐阅读