ComeçarComece de graça

Filtering Dask bags

The politician data you are working with comes from different sources, so it isn't very clean. Many of the dictionaries are missing keys that you may need to run your analysis. You will need to filter out the elements with important missing keys.

A function named has_birth_date() is available in the environment. It checks the input dictionary to see if it contains the key 'birth_date'. It returns True if the key is in the dictionary and False if not.

def has_birth_date(dictionary):
  return 'birth_date' in dictionary

The bag you created in the last exercise is available in your environment as dict_bag.

Este exercício faz parte do curso

Parallel Programming with Dask in Python

Ver curso

Instruções do exercício

  • Use dict_bag's .count() method to print out the number of elements it contains.
  • Use the has_birth_date() function to filter out the elements which do not have the 'birth_date' key.
  • Print out the number of elements filtered_bag contains.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Print the number of elements in dict_bag
print(____)

# Filter out records using the has_birth_date() function
filtered_bag = dict_bag.____(____)

# Print the number of elements in filtered_bag
print(____)
Editar e executar o código