データフレームを作成する

pandas によるデータ操作

Maggie Matsui

Senior Content Developer at DataCamp

辞書

my_dict = {
    "key1": value1,
    "key2": value2,
    "key3": value3
}
my_dict["key1"]
value1
my_dict = {
    "title": "Charlotte's Web",
    "author": "E.B. White",
    "published": 1952
}
my_dict["title"]
Charlotte's Web
pandas によるデータ操作

データフレームを作成する

辞書のリストから

  • 行ごとに構築

複数の辞書が角括弧内に表示されており、辞書のリストを表しています。

リストの辞書から

  • 列ごとに構築

辞書の中に複数の角括弧の組が表示され、リストの辞書を表しています。

pandas によるデータ操作

行ごとの辞書一覧

名前 犬種 体高 (cm) 体重 (kg) 生年月日
ジンジャー ダックスフンド 22 10 2019-03-14
スカウト ダルメシアン 59 25 2019-05-09
list_of_dicts = [

{"name": "Ginger", "breed": "Dachshund", "height_cm": 22, "weight_kg": 10, "date_of_birth": "2019-03-14"},
{"name": "Scout", "breed": "Dalmatian", "height_cm": 59, "weight_kg": 25, "date_of_birth": "2019-05-09"}
]
pandas によるデータ操作

行ごとの辞書一覧

名前 犬種 体高 (cm) 体重 (kg) 生年月日
ジンジャー ダックスフンド 22 10 2019-03-14
スカウト ダルメシアン 59 25 2019-05-09
new_dogs = pd.DataFrame(list_of_dicts)
print(new_dogs)
     name      breed  height_cm  weight_kg date_of_birth
0  Ginger  Dachshund         22         10    2019-03-14
1   Scout  Dalmatian         59         25    2019-05-09
pandas によるデータ操作

列ごとのリストの辞書

名前 品種 体高 体重 生年月日
ジンジャー ダックスフンド 22 10 2019-03-14
スカウト ダルメシアン 59 25 2019-05-09
  • キー = 列名
  • = 列の値の一覧
dict_of_lists = {

"name": ["Ginger", "Scout"],
"breed": ["Dachshund", "Dalmatian"],
"height_cm": [22, 59],
"weight_kg": [10, 25],
"date_of_birth": ["2019-03-14", "2019-05-09"]
}
new_dogs = pd.DataFrame(dict_of_lists)
pandas によるデータ操作

列ごとのリストの辞書

名前 犬種 体高 (cm) 体重 (kg) 生年月日
ジンジャー ダックスフンド 22 10 2019-03-14
スカウト ダルメシアン 59 25 2019-05-09
print(new_dogs)
     name      breed  height_cm  weight_kg date_of_birth
0  Ginger  Dachshund         22         10    2019-03-14
1   Scout  Dalmatian         59         25    2019-05-09
pandas によるデータ操作

練習しましょう!

pandas によるデータ操作

Preparing Video For Download...