始める無料で始める

DataFrame への変換

政治家の JSON データから DataFrame を作成したいとします。データの入れ子を解除できたので、あとは DataFrame の列として残したいキーを選ぶだけです。

前の演習で作成した Dask bag は、dict_bag として環境に用意されています。

この演習はコースの一部です

Pythonで学ぶDaskによる並列プログラミング

コースを見る

演習の手順

  • select_keys() 関数を完成させ、keys_to_keep リストにあるキーを新しいフィルタ済み辞書に追加して返すようにしてください。
  • select_keys() 関数を使って、dict_bag から ['gender','name', 'birth_date', 'url'] のキーを選択します。
  • フィルタした bag を Dask DataFrame に変換します。
  • DataFrame の先頭の数行を表示します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

def select_keys(dictionary, keys_to_keep):
  new_dict = {}
  # Loop through kept keys and add them to new dictionary
  for k in ____:
    ____
  return new_dict

# Use the select_keys to reduce to the 4 required keys
filtered_bag = dict_bag.map(____, ____=____)

# Convert the restructured bag to a DataFrame
df = filtered_bag.____

# Print the first few rows of the DataFrame
print(____)
コードを編集して実行