nlftp.mlit.go.jp
import pandas as pd
import geopandas as gpd
ehime = gpd.read_file("A27-21_38_GML.zip")
imabari = (
ehime[ehime["A27_001"] == "38202"]
.copy()
.reset_index(drop=True)
.rename(columns={"A27_004": "学校名", "A27_005": "住所"})
)
imabari
imabari.plot()
df0 = pd.read_csv(
"https://www.city.imabari.ehime.jp/opendata/data/school.csv",
encoding="cp932",
dtype={"電話番号": str},
)
df0
df1 = df0[df0["施設名"].str.endswith("小学校")].copy()
df1["施設名"] = df1["施設名"].str.replace("今治市立", "")
df1
df2 = (
pd.read_html(
"https://www.city.imabari.ehime.jp/gakukyou/shogakkou.html", index_col=0
)[0]
.fillna(0)
.astype(str)
)
df2
df3 = df2.applymap(lambda s: s.replace("人", "").replace(",", ""))
df3 = df3.astype(int).drop("合計")
df3
df4 = df3.sort_values(by=["計"], ascending=False).copy()
df4.head(13)
df4.tail(13)
pd.options.plotting.backend = "plotly"
df5 = df4.drop("計", axis=1).copy()
df5
df5.plot.barh()
import folium
school = pd.merge(imabari, df3, on="学校名")
school
map = folium.Map(
location=[34.0662403, 132.9976865],
zoom_start=14,
tiles="https://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png",
attr='© <a href="https://maps.gsi.go.jp/development/ichiran.html">国土地理院</a>',
)
folium.Choropleth(
geo_data=school.to_json(),
data=school,
columns=["学校名", "計"],
key_on="properties.学校名",
threshold_scale=[0, 50, 100, 200, 300, 400, 500, 1000],
fill_opacity=0.4,
fill_color="BuPu",
).add_to(map)
for i, r in df1.iterrows():
folium.Marker(
location=[r["緯度"], r["経度"]], popup=r["施設名"], icon=folium.Icon(color="red")
).add_to(map)
map
map.save("map.html")