Converting to DataFrame
You want to make a DataFrame out of the politician JSON data. Now that you have de-nested the data, all you need to do is select the keys to keep as columns in the DataFrame.
The Dask bag you created in the last exercise is available in your environment as dict_bag.
Latihan ini adalah bagian dari kursus
Parallel Programming with Dask in Python
Petunjuk latihan
- Complete the
select_keys()function so that the keys in thekeys_to_keeplist are added to the new filtered dictionary and returned. - Use the
select_keys()function to select the keys['gender','name', 'birth_date', 'url']fromdict_bag. - Convert the filtered bag to a Dask DataFrame.
- Print the first few rows of the DataFrame.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
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(____)