開始使用免費開始

轉換為 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(____)
編輯並執行程式碼