LoginSignup
0
0

More than 3 years have passed since last update.

辞書型リストとDataFrameの相互変換

Posted at

辞書型リストをDataFrameに変換して処理したい

もう何度も調べたくない・・・。そんな自分へ備忘録です。
みなさんのお役にも立てればいいな・・・

環境情報

・python: 3.7.3
・pandas: 1.0.3

実装サンプル

辞書型リストとDataFrameの相互変換.py

import pandas as pd

dict_list = [
    {"A": "a1", "B": "b1", "C": "c1"},
    {"A": "a2", "B": "b2", "C": "c2"},
    {"A": "a3", "B": "b3", "C": "c3"}
]

# 辞書型リスト -> DataFrameへ変換
df = pd.DataFrame(dict_list)
print(df)
#     A   B   C
# 0  a1  b1  c1
# 1  a2  b2  c2
# 2  a3  b3  c3

# DataFrame -> 辞書型リストへ変換
dfToList = list(df.to_dict('index').values())
print(dfToList)
# [{'A': 'a1', 'B': 'b1', 'C': 'c1'}, 
#  {'A': 'a2', 'B': 'b2', 'C': 'c2'}, 
#  {'A': 'a3', 'B': 'b3', 'C': 'c3'}]

以上、終わり。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0