串联操作
现在您已经完成数据的加载和清洗,可以开始进行分析。您的第一个任务是查看政客的出生日期。出生日期是类似 '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(____(____))