Question
Create a function calledarea_expenditure_counts(data, lower_spent, upper_spent)which creates a dictionary of the number of expenditures in the given expenditure amount bracket for each area. That is,
Create a function calledarea_expenditure_counts(data, lower_spent, upper_spent)which creates a dictionary of the number of expenditures in the given expenditure amount bracket for each area. That is, each key in the dictionary should be an area name, and the value for that area should be anintcorresponding to the number of expenditures in the area who fall in the expenditure amount bracket specified bylower_spentandupper_spent(inclusive). Your dictionary should have a key for all areas inVALID_AREAS, even the ones that have no expenditures in the expenditure amount bracket. The function should return the top 5 areas by expenditure count as a list of tuples[(area, expenditure_count), ...]. Top 5 areas should be listed indescending order by count, and ties should be broken byalphabetical order.
In this question, ignore any nested dictionary with aNonevalue for theareakey or theexpenditurekey.Nonevalues for other keys are acceptable. You may assume thatlower_spentandupper_spentare positive. Iflower_spent > upper_spent, your function should return a list with a value of 0 for every expenditure.
Here are some examples of how your function should behave:
>>> data_cleaned = read_data("cleaned_data.csv")
>>> area_expenditure_counts(data_cleaned, 1000, 5000)
[('Medical services', 15), ('Public hospitals', 15),
('Benefit-paid pharmaceuticals', 6), ('All other medications', 4),
('Dental services', 3)]
>>>
>>> area_expenditure_counts(data_cleaned, 0, 1000)
[('Patient transport services', 88), ('Dental services', 85),
('Private hospitals', 82), ('Aids and appliances', 81),
('Public hospitals', 80)]
>>>
>>> area_expenditure_counts(data_cleaned, 4000, 8000)
[('Public hospitals', 3), ('Medical services', 2), ('Administration', 0),
('Aids and appliances', 0), ('All other medications', 0)]
>>>
>>> area_expenditure_counts(data_cleaned, -1000, 0)
[('Community health', 22), ('Medical expense tax rebate', 20),
('Patient transport services', 16), ('Dental services', 5),
('All other medications', 4)]
>>>
>>> area_expenditure_counts(data_cleaned, 5000, 1000)
[('Administration', 0), ('Aids and appliances', 0),
('All other medications', 0), ('Benefit-paid pharmaceuticals', 0),
('Capital expenditure', 0)]
CODE:
from header import read_data, VALID_AREAS
data_cleaned = read_data("cleaned_data.csv")
print(data_cleaned)
def area_expenditure_counts(data, lower_spent, upper_spent):
# replace 'pass' with your code
pass
# to test your function with another CSV file,
# change the value of this variable
test_file = 'cleaned_data.csv'
# you don't need to modify the code below
if __name__ == '__main__':
data_cleaned = read_data(test_file)
sample of data_cleaned file:
{'10': {'fin_year': '1997-98', 'state': 'NSW', 'area': 'All other medications', 'funding': 'Non-government', 'detailed_funding': 'Other non-government', 'expenditure': '16'}, '100': {'fin_year': '1997-98', 'state': 'VIC', 'area': 'Public hospitals', 'funding': 'Government', 'detailed_funding': 'Australian Government', 'expenditure': '1983'}, '1009': {'fin_year': '1999-00', 'state': 'QLD', 'area': 'Public health', 'funding': 'Government', 'detailed_funding': 'Australian Government', 'expenditure': '116'}, '1015': {'fin_year': '1999-00', 'state': 'QLD', 'area': 'Public hospitals', 'funding': 'Non-government', 'detailed_funding': 'Other non-government', 'expenditure': '78'}, '1016': {'fin_year': '1999-00', 'state': 'QLD', 'area': 'Public hospitals', 'funding': 'Non-government', 'detailed_funding': 'Private health insurance funds', 'expenditure': '30'}, '1020': {'fin_year': '1999-00', 'state': 'WA', 'area': 'Administration', 'funding': 'Government', 'detailed_funding': 'Australian Government', 'expenditure': '145'}, '1033': {'fin_year': '1999-00', 'state': 'WA', 'area': 'Benefit-paid pharmaceuticals', 'funding': 'Non-government', 'detailed_funding': 'Individuals', 'expenditure': '62'}, '1036': {'fin_year': '1999-00', 'state': 'WA', 'area': 'Capital expenditure', 'funding': 'Non-government', 'detailed_funding': 'Other non-government', 'expenditure': '145'}, '1046': {'fin_year': '1999-00', 'state': 'WA', 'area': 'Dental services', 'funding': 'Non-government', 'detailed_funding': 'Private health insurance funds', 'expenditure': '103'}, '1065': {'fin_year': '1999-00', 'state': 'WA', 'area': 'Private hospitals', 'funding': 'Non-government', 'detailed_funding': 'Private health insurance funds', 'expenditure': '280'}, '1074': {'fin_year': '1999-00', 'state': 'WA', 'area': 'Public hospitals', 'funding': 'Non-government', 'detailed_funding': 'Private health insurance funds', 'expenditure': '27'}, '1078': {'fin_year': '1999-00', 'state': 'SA', 'area': 'Administration', 'funding': 'Government', 'detailed_funding': 'Australian Government', 'expenditure': '147'}, '1084': {'fin_year': '1999-00', 'state': 'SA', 'area': 'Aids and appliances', 'funding': 'Non-government', 'detailed_funding': 'Private health insurance funds', 'expenditure': '16'}, '1087': {'fin_year': '1999-00', 'state': 'SA', 'area': 'All other medications', 'funding': 'Non-government', 'detailed_funding': 'Other non-government', 'expenditure': '3'}, '1093': {'fin_year': '1999-00', 'state': 'SA', 'area': 'Capital expenditure', 'funding': 'Non-government', 'detailed_funding': 'Other non-government', 'expenditure': '57'}, '1107': {'fin_year': '1999-00', 'state': 'SA', 'area': 'Medical services', 'funding': 'Non-government', 'detailed_funding': 'Other non-government', 'expenditure': '110'}, '1114': {'fin_year': '1999-00', 'state': 'SA', 'area': 'Patient transport services', 'funding': 'Government', 'detailed_funding': 'State and local', 'expenditure': '52'}, '1116': {'fin_year': '1999-00', 'state': 'SA', 'area': 'Patient transport services', 'funding': 'Non-government', 'detailed_funding': 'Other non-government', 'expenditure': '8'}}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started