开始使用免费开始使用

重构字典

现在,您要清理政客数据,并将其放入 Dask DataFrame 中。不过,这些政客数据是嵌套结构,因此在放入 DataFrame 前,您还需要做一些预处理。

您想提取的一项数据埋藏在字典的多层结构中。这是每位政客对应的网站链接。下面的示例展示了它在字典中的存储方式。

record = {
...
 'links': [{'note': '...',
            'url': '...'},],  # Stored here
...
}

装有政客数据的 bag 在您的环境中名为 dict_bag

本练习是课程的一部分

Python 中的 Dask 并行编程

查看课程

练习说明

  • 完成 extract_url() 函数:从字典中提取 'links' 键下列表第 0 个元素内的 'url' 键,并将其赋给键 url
  • extract_url() 函数应用到 bag 的所有元素上。

交互式实操练习

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

def extract_url(x):
    # Extract the url and assign it to the key 'url'
    x['url'] = x[____][____][____]
    return x
  
# Run the function on all elements in the bag.
dict_bag = ____

print(dict_bag.take(1))
编辑并运行代码