LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Parallel Programming with Dask in Python

Kurs anzeigen

Anleitung zur Übung

  • Complete the select_keys() function so that the keys in the keys_to_keep list are added to the new filtered dictionary and returned.
  • Use the select_keys() function to select the keys ['gender','name', 'birth_date', 'url'] from dict_bag.
  • Convert the filtered bag to a Dask DataFrame.
  • Print the first few rows of the DataFrame.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

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(____)
Code bearbeiten und ausführen