การแปลงเป็น DataFrame
เราต้องการสร้าง DataFrame จากข้อมูล JSON ของนักการเมือง เมื่อ de-nest ข้อมูลเรียบร้อยแล้ว ขั้นตอนต่อไปคือเลือกคีย์ที่ต้องการใช้เป็นคอลัมน์ใน DataFrame
Dask bag ที่สร้างไว้ในแบบฝึกหัดที่แล้วพร้อมใช้งานในชื่อตัวแปร dict_bag
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Parallel Programming with Dask in Python
คำแนะนำการฝึกหัด
- เติมโค้ดในฟังก์ชัน
select_keys()ให้คีย์ที่อยู่ในkeys_to_keepถูกเพิ่มลงในดิกชันนารีที่กรองแล้วและส่งค่ากลับ - ใช้ฟังก์ชัน
select_keys()เพื่อเลือกคีย์['gender','name', 'birth_date', 'url']จากdict_bag - แปลง 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(____)