开始使用免费开始使用

转换为 DataFrame

您希望把政客的 JSON 数据转换为一个 DataFrame。既然已经对数据进行了去嵌套处理,接下来只需选择要保留为 DataFrame 列的键即可。

您在上一个练习中创建的 Dask bag 已在环境中提供,名称为 dict_bag

本练习是课程的一部分

Python 中的 Dask 并行编程

查看课程

练习说明

  • 完成 select_keys() 函数,将 keys_to_keep 列表中的键添加到新的筛选字典中并返回。
  • 使用 select_keys() 函数,从 dict_bag 中选择键 ['gender','name', 'birth_date', 'url']
  • 将筛选后的 bag 转换为 Dask DataFrame。
  • 打印该 DataFrame 的前几行。

交互式实操练习

通过完成这段示例代码来试试这个练习。

def select_keys(dictionary, keys_to_keep):
  new_dict = {}
  # Loop through kept keys and add them to new dictionary
  for k in ____:
    ____
  return new_dict

# Use the select_keys to reduce to the 4 required keys
filtered_bag = dict_bag.map(____, ____=____)

# Convert the restructured bag to a DataFrame
df = filtered_bag.____

# Print the first few rows of the DataFrame
print(____)
编辑并运行代码