過濾 Dask bag
你正在處理的政治人物資料來自不同來源,因此並不乾淨。許多字典都缺少你進行分析可能需要的鍵。你需要把缺少重要鍵的元素過濾掉。
環境中提供了一個名為 has_birth_date() 的函式。它會檢查輸入的字典是否包含鍵 'birth_date'。如果字典中有這個鍵則回傳 True,否則回傳 False。
def has_birth_date(dictionary):
return 'birth_date' in dictionary
你在上一個練習中建立的 bag 已載入為 dict_bag,可直接在環境中使用。
本練習屬於課程
在 Python 中使用 Dask 進行平行程式設計
練習說明
- 使用
dict_bag的.count()方法,印出其包含的元素數量。 - 使用
has_birth_date()函式,將不包含'birth_date'鍵的元素過濾掉。 - 印出
filtered_bag所包含的元素數量。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Print the number of elements in dict_bag
print(____)
# Filter out records using the has_birth_date() function
filtered_bag = dict_bag.____(____)
# Print the number of elements in filtered_bag
print(____)