Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Final Quiz
7. Question 6
Summary

Instruction

And finally, the last question!

Exercise

A study was conducted to answer the following question:

Do people save more when they are more educated or when they get older?

Participants were asked to provide their age group, education level, and current savings. All answers are stored in a CSV file named savings.csv:

age_group,education,savings
26-30,primary,6965
31-35,primary,5520
36-40,secondary,24606
41+,tertiary,82387
...

Your task is to calculate the average savings for each study group. A study group is a combination of an education level and age group. The end result should be stored in a CSV file named savings_report.csv and should look like this (random average values are provided as examples):

primary (26-30),3450.34
primary (31-35),1230.0
primary (36-40),2519.2
primary (41+),3746.81
...
secondary (31-35),13102.46
secondary (36-40),15670.73
...

You don't need to sort the records in the output file in any specific order.

Stuck? Here's a hint!

  1. Create two empty dictionaries: groups_count and groups_sum. The first one will count the number of participants in each group; the second one will store the sum of savings in each group.
  2. Open the input file. For each row, do the following:
    1. Construct the name of the group as will be seen in the output file:
      group_name = row['education'] + ' (' + row['age_group'] + ')'
    2. Check if group_name is an existing key in either groups_count or groups_sum. If it is, add one to the value in group_count, and add the savings field value to the groups_sum value. If not, create a new key in both dictionaries; set their values to 1 and int(row['savings']), respectively.
  3. Create the output file. Use the following for loop:
    for key, value in groups_sum.items():
    Inside the loop, create a list of two fields:
    1. The key from groups_sum.
    2. The average, calculated as the value in groups_sum divided by the value in groups_count for the given key.