操作をつなげて実行する
データの読み込みとクリーニングが終わったので、分析を始めましょう。最初のタスクは政治家の生年月日を調べることです。生年月日は 'YYYY-MM-DD' のような文字列で、先頭の4文字が年です。
前の演習で作成したフィルタ済みの Dask bag filtered_bag は、環境内で利用できます。
この演習はコースの一部です
Pythonで学ぶDaskによる並列プログラミング
演習の手順
- Bag の
.pluck()メソッドを使って'birth_date'の文字列を取り出します。 - ラムダ関数を書いて、
'birth_date'の文字列から年の部分を取り出し、整数に変換します。 - 新しい Bag
birth_year_bagを使って、生年の最小値・最大値・平均を計算します。 dask.compute()関数を使って、これら3つの集計を効率的に計算します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Select the 'birth_date' from each dictionary in the bag
birth_date_bag = filtered_bag.____
# Extract the year as an integer from the birth_date strings
birth_year_bag = birth_date_bag.____(lambda x: ____)
# Calculate the min, max and mean birth years
min_year = ____
max_year = ____
mean_year = ____
# Compute the results efficiently and print them
print(____(____))