I need help writing a for loop to add number of times an element appears in a dataset to the value of a dictionary comprehension.
Here is the sample dataset:
salary_data =
{'Age': '39', 'Education': 'E - Bachelors', 'Occupation': 'Adm-clerical', 'Relationship': 'Not-in-family', 'Race': 'White', 'Sex': 'Male', 'Target': '<=50K'}
{'Age': '50', 'Education': 'E - Bachelors', 'Occupation': 'Exec-managerial', 'Relationship': 'Husband', 'Race': 'White', 'Sex': 'Male', 'Target': '<=50K'}
{'Age': '38', 'Education': 'B - HS Diploma', 'Occupation': 'Handlers-cleaners', 'Relationship': 'Not-in-family', 'Race': 'White', 'Sex': 'Male', 'Target': '<=50K'}
{'Age': '53', 'Education': 'A - No HS Diploma', 'Occupation': 'Handlers-cleaners', 'Relationship': 'Husband', 'Race': 'Black', 'Sex': 'Male', 'Target': '<=50K'}
{'Age': '28', 'Education': 'E - Bachelors', 'Occupation': 'Prof-specialty', 'Relationship': 'Wife', 'Race': 'Black', 'Sex': 'Female', 'Target': '<=50K'}
{'Age': '37', 'Education': 'F - Graduate Degree', 'Occupation': 'Exec-managerial', 'Relationship': 'Wife', 'Race': 'White', 'Sex': 'Female', 'Target': '<=50K'}
{'Age': '49', 'Education': 'A - No HS Diploma', 'Occupation': 'Other-service', 'Relationship': 'Not-in-family', 'Race': 'Black', 'Sex': 'Female', 'Target': '<=50K'}
{'Age': '52', 'Education': 'B - HS Diploma', 'Occupation': 'Exec-managerial', 'Relationship': 'Husband', 'Race': 'White', 'Sex': 'Male', 'Target': '>50K'}
{'Age': '31', 'Education': 'F - Graduate Degree', 'Occupation': 'Prof-specialty', 'Relationship': 'Not-in-family', 'Race': 'White', 'Sex': 'Female', 'Target': '>50K'}
{'Age': '42', 'Education': 'E - Bachelors', 'Occupation': 'Exec-managerial', 'Relationship': 'Husband', 'Race': 'White', 'Sex': 'Male', 'Target': '>50K'}
and a list of unique education levels was given:
unique_education_levels=
['A - No HS Diploma',
'B - HS Diploma',
'C - Some College',
'D - Associates',
'E - Bachelors',
'F - Graduate Degree']
I need to create a dictionary called education_level_frequencies where the keys are the unique education levels and the values are the number of times the education level appears in the dataset.
So far I used a dictionary comprehension to create the dictionary with values of 0.
education_level_frequencies = [{level: 0} for level in unique_education_levels]
I'm trying to use a for loop to iterate through the dataset and add +1 to the education_level_frequencies keys to no avail.
for entry in salary_data:
if entry['Education'] == education_level_frequencies:
education_level_frequencies[entry] += 1