串接操作
現在你已經載入並清理好資料,可以開始進行分析。第一個任務是查看政治人物的出生日期。出生日期是像 'YYYY-MM-DD' 這樣的字串格式。字串的前 4 個字元代表年份。
你在上一個練習建立的過濾後 Dask bag,filtered_bag,已經在你的環境中可用。
本練習屬於課程
在 Python 中使用 Dask 進行平行程式設計
練習說明
- 使用 bag 的
.pluck()方法擷取'birth_date'字串。 - 撰寫一個 lambda 函式,從
'birth_date'字串中擷取年份字串,並將其轉換為整數。 - 使用新的 bag
birth_year_bag計算出生年份的最小值、最大值與平均值。 - 使用
dask.compute()函式有效率地計算這三個彙總值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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(____(____))