www.health-net.or.jp
import pandas as pd
dfs = pd.read_html("http://www.health-net.or.jp/tobacco/product/pd090000.html", header=0)
df = dfs[0]
df.columns
df.rename(columns={'Unnamed: 1':"性別"}, inplace=True)
df
df["年"] = df.index // 2 + 1965
df.drop("年度", axis=1, inplace=True)
df1 = df.pivot(index="年", columns="性別")
df1
df1.columns
df2 = df1.swaplevel(0, 1, axis=1)
df3 = df2[["男", "女"]]
df3
グラフ
import matplotlib.pyplot as plt
import japanize_matplotlib
df3.plot(title="年代別喫煙率の推移")
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()