Created
July 28, 2018 04:25
-
-
Save kohiro37/9c501d7274da7f2efcdcdb88ed61f483 to your computer and use it in GitHub Desktop.
Example of e-Stat API usage in Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
import json | |
import pandas as pd | |
from pandas.io.json import json_normalize | |
# e-Stat APIのappId | |
appId = 'コピーしておいたappIdをここにペーストする' | |
# APIのリクエストURL | |
# httpsにしておく | |
url = 'https://api.e-stat.go.jp/rest/2.1/app/getStatsData?cdCat03=47001&cdCat04=48001&cdArea=00000%2C01000%2C02000%2C03000%2C04000%2C05000%2C06000%2C07000%2C08000%2C09000%2C10000%2C11000%2C12000%2C13000%2C14000%2C15000%2C16000%2C17000%2C18000%2C19000%2C20000%2C21000%2C22000%2C23000%2C24000%2C25000%2C26000%2C27000%2C28000%2C29000%2C30000%2C31000%2C32000%2C33000%2C34000%2C35000%2C36000%2C37000%2C38000%2C39000%2C40000%2C41000%2C42000%2C43000%2C44000%2C45000%2C46000%2C47000&cdCat02=H1000%2CH1010%2CH1050&cdCat01=07001&appId=&lang=J&statsDataId=0003018185&metaGetFlg=Y&cntGetFlg=N§ionHeaderFlg=1' | |
def url_json(url): | |
""" | |
URLをJSON取得用に変更する | |
「/apps/」のあとに「json/」を追加 | |
""" | |
return url.replace('/app/', '/app/json/') | |
def set_appid(url): | |
""" | |
URLにappIdを設定する | |
「appId=」のあとにappIdを追加 | |
""" | |
return url.replace('appId=', 'appId=' + appId) | |
def to_dataframe(res): | |
item = [] # 品目のリスト | |
pref = [] # 都道府県のリスト | |
for obj in res['GET_STATS_DATA']['STATISTICAL_DATA']['CLASS_INF']['CLASS_OBJ']: | |
# 分類などの出力 | |
if isinstance(obj['CLASS'], dict): # dict | |
print(obj['@id'], obj['@name'], obj['CLASS']['@name']) | |
else: # list | |
id = obj['@id'] | |
for cat in obj['CLASS']: | |
print(id, obj['@name'], cat['@name'], cat['@code']) | |
if id == 'cat02': # 2009品目分類 | |
item.append(cat['@name']) | |
elif id == 'area': # 2009地域 | |
pref.append(cat['@name']) | |
# JSONをDataFrameに変換 | |
#df = pd.read_json(res['GET_STATS_DATA']['STATISTICAL_DATA']['DATA_INF']['VALUE']) | |
df = json_normalize(res['GET_STATS_DATA']['STATISTICAL_DATA']['DATA_INF']['VALUE']) | |
# 必要なカラムのみ抽出 | |
df = df.loc[:, ['$', '@area', '@cat02']] | |
# 都道府県をインデックス、品目(緑茶、紅茶、コーヒー)をカラムにする | |
g = df.pivot(index='@area', columns='@cat02') | |
g.index = pref | |
g.columns = item | |
g = g.iloc[1:48] # 「全国」の行を省く | |
return g | |
def get_estat_json(): | |
# JSON取得用にURLを変更し、appIdを設定する | |
request_url = set_appid(url_json(url)) | |
with urllib.request.urlopen(request_url) as f: | |
res = json.loads(f.read().decode('utf8')) | |
return res | |
def main(): | |
res = get_estat_json() | |
df = to_dataframe(res) | |
print(df) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment