下記の日銀のページの主要統計データ表からデータを取得するコードです。
https://www.stat-search.boj.or.jp/index.html
全てのデータを取得する
import pandas as pd
import japanize_matplotlib
import matplotlib.pyplot as plt
url=[
'https://www.stat-search.boj.or.jp/ssi/mtshtml/bp01_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/pr01_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/ff_q_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/bs02_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/md11_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/md13_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/md01_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/md02_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/fm02_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/fm08_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/fm09_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/ir01_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/ir04_m_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_1.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_2.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_3.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_4.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_5.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_6.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_7.html',
'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_8.html'
]
for i in range(len(url)):
# データ取得
tbl = pd.read_html(url[i], header=[1], skiprows=(2,3,4,5,6), encoding="shift-jis")
tbl2 = pd.read_html(url[i], header=[0], skiprows=(2,3,4,5,6), encoding="shift-jis")
# データ取得が正しく行われているか確認
print(f"URL: {url[i]}")
print(f"Columns: {tbl[0].columns}") # カラム名の確認
# 0個目のテーブルを格納
df_tmp = pd.DataFrame(tbl[0])
try:
# 日付をインデックスに指定
df_tmp.rename(columns={'系列名称':'Date'}, inplace=True)
df_tmp = df_tmp.set_index('Date')
# 日付のフォーマットを自動推定
df_tmp.index = pd.to_datetime(df_tmp.index) # 自動的に日付形式を推定
df_tmp = df_tmp.replace('ND', 0)
df_tmp = df_tmp.astype(float)
# 名称出力、URL出力
print(str(format(i,'02')) + "_" + tbl2[0].columns[1])
print(url[i])
# グラフ作成処理
df_tmp.tail(100).plot(figsize=(12,4))
plt.legend(bbox_to_anchor=(1.01, 1.0), loc='upper left')
plt.show()
except KeyError as e:
print(f"KeyError: {e}. The column might be missing or named differently.")
print(f"Available columns: {df_tmp.columns}")
except ValueError as e:
print(f"ValueError: {e}. There was an issue with the date format.")
指定したデータのみ取得する
import pandas as pd
import japanize_matplotlib
import matplotlib.pyplot as plt
# URLとターゲットカラムを指定
url = 'https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_8.html'
target_column = 'D.I./貸出態度/大企業/全産業/実績'
# データ取得
tbl = pd.read_html(url, header=[1], skiprows=(2,3,4,5,6), encoding="shift-jis")
# 0個目のテーブルを格納
df_tmp = pd.DataFrame(tbl[0])
# 日付をインデックスに指定
df_tmp.rename(columns={'系列名称':'Date'}, inplace=True)
df_tmp = df_tmp.set_index('Date')
# 日付のフォーマットを自動推定
df_tmp.index = pd.to_datetime(df_tmp.index) # 自動的に日付形式を推定
# 指定されたカラムのみを選択
df_selected = df_tmp[[target_column]]
# グラフ作成処理
df_selected.plot(figsize=(12,4), title='D.I./貸出態度/大企業/全産業/実績')
plt.legend(loc='upper left')
plt.show()
URLと対応するカラム名一覧
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/bp01_m_1.html
Columns: 系列名称, 経常収支, 金融収支/ネット
データ: 国際収支(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/pr01_m_1.html
Columns: 系列名称, [国内企業物価指数] 総平均(前年比), [輸出物価指数/円ベース] 総平均(前年比), [輸入物価指数/円ベース] 総平均(前年比), [連鎖方式による国内企業物価指数] 総平均(前年比), [国内企業物価指数] 総平均, (参考)夏季電力料金調整後/総平均, [輸出物価指数/円ベース] 総平均, [輸入物価指数/円ベース] 総平均, [連鎖方式による国内企業物価指数] 総平均
データ: 企業物価指数(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/ff_q_1.html
Columns: 系列名称, 資産・合計/家計/ストック
データ: 家計金融資産(資金循環統計)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/bs02_m_1.html
Columns: 系列名称, 貸出金 前年比/資産, 実質預金 前年比/負債
データ: 主要勘定 前年比(国内銀行)(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/md11_m_1.html
Columns: 系列名称, 貸出金/末残/銀行勘定、信託勘定、海外店勘定合計/国内銀行, 貸出金/法人(含む金融)/末残/銀行勘定、信託勘定、海外店勘定合計/国内銀行, 貸出金/法人(含む金融)/設備資金/末残/銀行勘定、信託勘定、海外店勘定合計/国内銀行, 貸出金/法人(含む金融)/中小企業/末残/銀行勘定、信託勘定、海外店勘定合計/国内銀行, 貸出金/個人/末残/銀行勘定、信託勘定、海外店勘定合計/国内銀行
データ: 預金・現金・貸出金(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/md13_m_1.html
Columns: 系列名称, 総貸出平残(銀行・信金計)(a), 総貸出平残(銀行計), 総貸出平残(銀行計)前年比, 総貸出平残/都銀等(b), 総貸出平残/地銀・地銀II, 総貸出平残/地銀, 総貸出平残/地銀II, 総貸出平残/信金(c), 総貸出平残/(参考)外銀(円貸出)
データ: 貸出・預金動向(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/md01_m_1.html
Columns: 系列名称, マネタリーベース平均残高(前年比), マネタリーベース平均残高/うち 日本銀行券発行高(前年比), マネタリーベース平均残高/うち 貨幣流通高(前年比), マネタリーベース平均残高/うち 日銀当座預金(前年比), マネタリーベース平均残高, マネタリーベース平均残高/うち 日本銀行券発行高, マネタリーベース平均残高/うち 貨幣流通高, マネタリーベース平均残高/うち 日銀当座預金
データ: マネタリーベース平均残高(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/md02_m_1.html
Columns: 系列名称, M2/平残前年比/マネーストック(2004年3月以前はマネーサプライ), M3/平残前年比/マネーストック(2004年3月以前はマネーサプライ), _M1/平残前年比/マネーストック(2004年3月以前はマネーサプライ), 広義流動性/平残前年比/マネーストック(2004年3月以前はマネーサプライ), __現金通貨/平残前年比/マネーストック(2004年3月以前はマネーサプライ), __預金通貨/平残前年比/マネーストック(2004年3月以前はマネーサプライ), _準通貨/平残前年比/マネーストック(2004年3月以前はマネーサプライ), _CD/平残前年比/マネーストック(2004年3月以前はマネーサプライ), M2/平/マネーストック, M3/平/マネーストック, _M1/平/マネーストック, 広義流動性/平/マネーストック, __現金通貨/平/マネーストック, __預金通貨/平/マネーストック, _準通貨/平/マネーストック, _CD/平/マネーストック
データ: マネーストック(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/fm02_m_1.html
Columns: 系列名称, 無担保コールレート・O/N 月末/金利, 無担保コールレート・O/N 月平均/金利
データ: コールレート(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/fm08_m_1.htm
Columns: 系列名称, 東京市場 ドル・円 スポット 17時時点/月末, 東京市場 ドル・円 スポット 17時時点/月中平均, 東京市場 ドル・円 スポット 中心相場/月末, 東京市場 ドル・円 スポット 中心相場/月中平均, 東京市場 ドル・円 スポット 月中最高値, 東京市場 ドル・円 スポット 月中最安値
データ: 為替相場(東京インターバンク相場)(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/fm09_m_1.html
Columns: 系列名称, 名目実効為替レート指数, 実質実効為替レート指数
データ: 実効為替レート(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/ir01_m_1.html
Columns: 系列名称, 基準割引率および基準貸付利率
データ: 基準貸付利率(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/ir04_m_1.html
Columns: 系列名称, 新規/短期/国内銀行, 新規/長期/国内銀行, 新規/総合/国内銀行, ストック/短期/国内銀行, ストック/長期/国内銀行
データ: 貸出約定平均金利(月次)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_1.html
Columns: 系列名称, D.I./業況/大企業/製造業/実績, D.I./業況/大企業/製造業/予測, D.I./業況/大企業/非製造業/実績, D.I./業況/大企業/非製造業/予測, D.I./業況/中堅企業/製造業/実績, D.I./業況/中堅企業/製造業/予測, D.I./業況/中堅企業/非製造業/実績, D.I./業況/中堅企業/非製造業/予測, D.I./業況/中小企業/製造業/実績, D.I./業況/中小企業/製造業/予測, D.I./業況/中小企業/非製造業/実績, D.I./業況/中小企業/非製造業/予測
データ: 全国短観・判断項目(業況)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_2.html
Columns: 系列名称, D.I./国内需給/大企業/製造業/実績
データ: 全国短観・判断項目(国内需給)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_3.html
Columns: 系列名称, D.I./販売価格/大企業/製造業/実績
データ: 全国短観・判断項目(販売価格)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_4.html
Columns: 系列名称, D.I./仕入価格/大企業/製造業/実績
データ: 全国短観・判断項目(仕入価格)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_5.html
Columns: 系列名称, D.I./生産設備/大企業/製造業/実績, D.I./生産設備/中堅企業/製造業/実績, D.I./生産設備/中小企業/製造業/実績
データ: 全国短観・判断項目(生産設備)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_6.html
Columns: 系列名称, D.I./雇用人員/大企業/全産業/実績, D.I./雇用人員/中堅企業/全産業/実績, D.I./雇用人員/中小企業/全産業/実績
データ: 全国短観・判断項目(雇用人員)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_7.html
Columns: 系列名称, D.I./資金繰り/大企業/全産業/実績, D.I./資金繰り/中堅企業/全産業/実績, D.I./資金繰り/中小企業/全産業/実績
データ: 全国短観・判断項目(資金繰り)(四半期)
URL: https://www.stat-search.boj.or.jp/ssi/mtshtml/co_q_8.html
Columns: 系列名称, D.I./貸出態度/大企業/全産業/実績, D.I./貸出態度/中堅企業/全産業/実績, D.I./貸出態度/中小企業/全産業/実績
データ: 全国短観・判断項目(貸出態度)(四半期)
参考