Python 3
import csv
#I want to find out the correlation between bmi and charges, if there are any correlation
#this mean we are looking at the BMI and the charges
bmi_list =
charges_list =
age_list =
gender_list =
import csv
#I want to find out the correlation between bmi and charges, if there are any correlation
#this mean we are looking at the BMI and the charges
bmi_list =
charges_list =
age_list =
gender_list =
with open(‘insurance.csv’) as csvfile:
insurance = csv.DictReader(csvfile)
for row in insurance:
bmi_list.append(float(row[‘bmi’]))
charges_list.append(float(row[‘charges’]))
age_list.append(int(row[‘age’]))
gender_list.append(row[‘sex’])
with open(‘insurance.csv’) as csvfile:
insurance = csv.DictReader(csvfile)
for row in insurance:
bmi_list.append(float(row[‘bmi’]))
charges_list.append(float(row[‘charges’]))
age_list.append(int(row[‘age’]))
gender_list.append(row[‘sex’])
print(insurance_statement(bmi_list[1], charges_list[1]))
def insurance_statement(bmi_list, charges_list):
return “my bmi is {} and my charge is {}”.format(bmi_list, charges_list)
print(insurance_statement(bmi_list[1], charges_list[1]))
my bmi is 33.77 and my charge is 1725.5523
average_bmi = sum(bmi_list)/len(bmi_list)
print(average_bmi)
30.66339686098665
average_cost = sum(charges_list) / len(charges_list)
print(average_cost)
13270.422265141247
#def average_female_cost(gender_list, charges_list):
#total_female_cost = 0
#female_count = 0
#for gender, charge_female in zip(gender_list, charges_list):
#if gender == ‘female’:
#total_female_cost += charge_female
#female_count += 1
#if female_count > 0:
#average_cost_female = total_female_cost / female_count
else:
#average_cost_female = 0
return average_cost_female
average_cost_f
def average_female_cost(gender_list, charges_list):
total_female_cost = 0
female_count = 0
for gender, charge_female in zip(gender_list, charges_list):
if gender == ‘female’:
total_female_cost += charge_female
female_count += 1
if female_count > 0:
average_cost_female = total_female_cost / female_count
else:
average_cost_female = 0
return average_cost_female
average_cost_f = average_female_cost(gender_list, charges_list)
print(f"The average charges for females are: {average_cost_f}")
The average charges for females are: 12569.57884383535
average_cost_male
def average_male_cost(gender_list, charges_list):
total_male_cost = 0
male_count = 0
for gender, charge in zip(gender_list, charges_list):
if gender == 'male':
total_male_cost += charge
male_count += 1
if male_count > 0:
average_cost_male = total_male_cost / male_count
else:
average_cost_male = 0
return average_cost_male
average_cost_male = average_male_cost(gender_list, charges_list)
print(f"The average charges for males are: {average_cost_male}“)
The average charges for males are: 13956.751177721893
print(f"the difference between male and female insurace are :{value_difference}”)
value_difference = average_cost_male - average_cost_f
print(f"the difference between male and female insurace are :{value_difference}")
the difference between male and female insurace are :1387.1723338865431