过滤 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(____)