ComeçarComece de graça

Using hierarchies for categorical data

In this exercise, you will create and use hierarchies to apply data generalization on the bachelors column of the US Adult Income dataset.

An initial dictionary containing the hierarchies is available for you as hierarchies. It holds three categories for the education types: Primary, Secondary and Higher; each has a list of the data's corresponding education values. Feel free to explore it in the interactive console.

We will create a new dictionary that will hold the generalized education information and use to replace the original values.

The dataset is available as income_df.

Este exercício faz parte do curso

Data Privacy and Anonymization in Python

Ver curso

Instruções do exercício

  • Initialize the education_hierarchy as an empty dictionary.
  • Complete the inner loop to assign the education type key as the value. For example {'Some-college': 'Higher education'}.
  • Apply education hierarchy generalization to the bachelors column, assigning the result to the new column bachelors_generalized.

Exercício interativo prático

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

# Initialize dictionary for each education category value
education_hierarchy = ____

# Create hierachy for each of the education category values
for (key,education_values) in hierarchies.items():
    for education in education_values:
        education_hierarchy[education] = ____

# Apply education_hierarchy generalization to bachelors
income_df['bachelors_generalized'] = ____

# See resulting dataset
print(income_df.head())
Editar e executar o código