Created
May 14, 2021 03:09
-
-
Save AnchorBlues/8a36ac7fbe8894330985ddd0dc148bf5 to your computer and use it in GitHub Desktop.
jsonファイルを読み込んでpandasのDataFrameを返すスクリプト
This file contains hidden or 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
from pandas import json_normalize | |
import json | |
def read_json__normalize(json_file, add_key_category: bool = True): | |
""" | |
jsonファイルを読みこんでpandasのテーブル形式で返す | |
出力されるテーブルの各列の意味は以下の通り | |
* key_category : キーが階層的になっている場合、その一番上の階層での文字列 | |
* key : キー | |
* value : 値 | |
* dtype : 値のデータ型 | |
""" | |
with open(json_file) as data_file: | |
d = json.load(data_file) | |
# df = json_normalize(d, 'result').assign(**d['status']) | |
tmp = json_normalize(d) | |
df = tmp.transpose().reset_index().rename( | |
columns={"index": "key", 0: "value"}) | |
df['dtype'] = tmp.dtypes.values | |
# df.sort_values(by='key', inplace=True) | |
if add_key_category: | |
# キーが「a.b」など階層的になっているとき、一番上の階層でのキー名(左記の例であれば「a」)を格納する | |
df.insert(0, "key_category", df['key'].str.split( | |
".").apply(lambda v: v[0] if len(v) >= 2 else "")) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考: