Visualisez le taux de réponse Internet et le taux de réponse au courrier en grattant Excel du statut de réponse par préfecture dans National Census 2020
import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import urljoin
url = "https://www.kokusei2020.go.jp/internet/"
r = requests.get(url)
r.raise_for_status()
soup = BeautifulSoup(r.content, "html.parser")
links = {}
for i in soup.find_all("span", text="Excel"):
link = urljoin(url, i.find_parent("a").get("href"))
m = re.search("census_answers_(pref|city)_\d{6}.xlsx", link)
if m:
links[m.group(1)] = link
links
import pandas as pd
df_pref = pd.read_excel(
links["pref"],
index_col=[0, 1],
header=None,
skiprows=9,
usecols=[1, 2, 3, 4, 5, 6, 7],
names=["code", "Préfectures", "Nombre de ménages H27", "Net", "Par mail", "Net率", "Par mail率"],
)
df_pref["Nombre de réponses"] = df_pref["Net"] + df_pref["Par mail"]
df_pref["Tarif net"] *= 100
df_pref["Tarif d'envoi"] *= 100
df_pref["Taux de réponse"] = df_pref["Tarif net"] + df_pref["Tarif d'envoi"]
df_pref.to_csv("pref.csv", encoding="utf_8_sig")
df_city = pd.read_excel(
links["city"],
index_col=[0, 1, 2],
header=None,
skiprows=9,
usecols=[1, 2, 3, 4, 5, 6, 7, 8],
names=["code", "Préfectures", "Municipalité", "Nombre de ménages H27", "Net", "Par mail", "Net率", "Par mail率"],
)
df_city["Nombre de réponses"] = df_city["Net"] + df_city["Par mail"]
df_city["Tarif net"] *= 100
df_city["Tarif d'envoi"] *= 100
df_city["Taux de réponse"] = df_city["Tarif net"] + df_city["Tarif d'envoi"]
df_city.to_csv("city.csv", encoding="utf_8_sig")
df_city
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import japanize_matplotlib
#résolution
import matplotlib as mpl
mpl.rcParams["figure.dpi"] = 200
df1 = df_pref.sort_index(ascending=False).reset_index(level="code", drop=True)
df1.loc[:, ["Tarif net", "Tarif d'envoi"]].plot.barh(stacked=True, figsize=(5, 10))
plt.legend(bbox_to_anchor=(1.05, 1), loc="upper left", borderaxespad=0, fontsize=8)
plt.savefig("01.png ", dpi=200, bbox_inches="tight")
plt.show()
Recommended Posts