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!
- Create two empty dictionaries:
groups_countandgroups_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. - Open the input file. For each row, do the following:
- Construct the name of the group as will be seen in the output file:
group_name = row['education'] + ' (' + row['age_group'] + ')' - Check if
group_nameis an existing key in eithergroups_countorgroups_sum. If it is, add one to the value ingroup_count, and add the savings field value to thegroups_sumvalue. If not, create a new key in both dictionaries; set their values to 1 andint(row['savings']), respectively.
- Construct the name of the group as will be seen in the output file:
- Create the output file. Use the following
forloop:for key, value in groups_sum.items():
Inside the loop, create a list of two fields:- The
keyfromgroups_sum. - The average, calculated as the value in
groups_sumdivided by the value ingroups_countfor the given key.
- The



