Hi again!
I’m sorry to bother again, but I’m super stuck on this one.
I am looking at US Medical Insurance and would like to analyse the cost of the insurance but with a few conditions attached. For example I would like to select only men, non-smokers and living in the same region.
But I can’t manage to iterate through the dictionary to create these conditions and get the cost of the people who meet these specific conditions…
I leave the code below. I paste almost every line of the code, but the problem is the function inside the Class.
Thanks in advance!
ages = load_list_data(ages, "C:/Users/rodri.DESKTOP-RAT8004/Desktop/insurance.csv", "age")
sexes = load_list_data(sexes, "C:/Users/rodri.DESKTOP-RAT8004/Desktop/insurance.csv", "sex")
bmi = load_list_data(bmi, "C:/Users/rodri.DESKTOP-RAT8004/Desktop/insurance.csv", "bmi")
num_of_children = load_list_data(num_of_children, "C:/Users/rodri.DESKTOP-RAT8004/Desktop/insurance.csv", "children")
smoker_status = load_list_data(smoker_status, "C:/Users/rodri.DESKTOP-RAT8004/Desktop/insurance.csv", "smoker")
regions = load_list_data(regions, "C:/Users/rodri.DESKTOP-RAT8004/Desktop/insurance.csv", "region")
insurance_cost = load_list_data(insurance_cost, "C:/Users/rodri.DESKTOP-RAT8004/Desktop/insurance.csv", "charges")
def create_dictionary(ages, sexes, bmi, num_of_children, smoker_status, regions, insurance_cost):
patients = dict()
num_patients = len(ages)
for i in range(num_patients):
patients[i] ={"Age" : ages[i],
"Sex" : sexes[i],
"BMI" : bmi[i],
"Number of children" : num_of_children[i],
"Smoker" : smoker_status[i],
"Region" : regions[i],
"Insurance charge" : insurance_cost_int[i]
}
return patients
patients = create_dictionary(ages, sexes, bmi, num_of_children, smoker_status, regions, insurance_cost)
class PatientsInfo:
# antes de definir cualquier función tenemos que rellenar la clase con las listas que vamos a utiliar y definir la función
# __init__ con el argumento self asociado a cada lista
def __init__(self, patients_ages, patients_sexes, patients_bmi, patients_num_of_children,
patients_smoker_status, patients_regions, patients_insurance_cost, patients_patients):
self.patients_ages = patients_ages
self.patients_sexes = patients_sexes
self.patients_bmi = patients_bmi
self.patients_num_of_children = patients_num_of_children
self.patients_smoker_status = patients_smoker_status
self.patients_regions = patients_regions
self.patients_insurance_cost = patients_insurance_cost
self.patients_patients = patients_patients
def analyze_cost_by_sex(self):
total_male_cost = 0
total_female_cost = 0
for sex in self.patients_patients.values():
gender = self.patients_patients["Sex"]["male"]
if sex == gender:
total_male_cost += float(self.patients_patients["charges"])
else:
total_female_cost += float(self.patients_patients["charges"])
print("Average male cost: " + str(total_male_cost/len(total_male_cost))
print("Average female cost: " + str(total_female_cost/len(total_female_cost))
Really thanks to anyone solving this!