Get startedGet started for free

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.

This exercise is part of the course

Parallel Programming with Dask in Python

View Course

Exercise instructions

  • 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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(____)
Edit and Run Code