#!/usr/bin/env python
coding: utf-8
# U.S. Medical Insurance Costs
In[5]:
#develop lists
import csv
with open(‘insurance.csv’) as insurance_data:
key_data = insurance_data.readline().strip(’\n’)
key_list = key_data.split(’,’)
#print(key_list)
age =
sex =
bmi =
children =
smoker =
region =
charges =
for line in insurance_data:
values_data = insurance_data.readline().strip(’\n’)
values_list = values_data.split(’,’)
age.append(int(values_list[0]))
sex.append(values_list[1])
bmi.append(float(values_list[2]))
children.append(int(values_list[3]))
smoker.append(values_list[4])
region.append(values_list[5])
charges.append(float(values_list[6]))
In[6]:
Calculate Average Age
def average_number(lst):
total = 0
for i in lst:
total += i
return round((total / len(lst)))
print(“Average Age of patients”, average_number(age))
In[7]:
#Count data
def count_data(type_data, lst):
count = 0
for i in lst:
if type_data == i:
count += 1
return count
#count total females and male
print(“Number of female patients”, count_data(‘female’,sex), ‘Number of male patients’, count_data(‘male’,sex))
In[9]:
#Count most smoker based on any input variable
print(“Number of smokers”, count_data(‘yes’,smoker), ‘Number of non-smokers’, count_data(‘no’,smoker))
Count Female smokers and male smokers
count_female_smoker = 0
count_male_smoker = 0
lst_sex_smoker = list(zip(sex, smoker))
for patient in lst_sex_smoker:
if patient[0] == ‘female’ and patient[1] == ‘yes’:
count_female_smoker += 1
elif patient[0] == ‘male’ and patient[1] == ‘yes’:
count_male_smoker += 1
print(‘Female Smokers’, count_female_smoker, count_female_smoker100/count_data(‘yes’,smoker),’% of Total smokers’)
print(‘Male Smokers’, count_male_smoker, count_male_smoker100/count_data(‘yes’,smoker),’% of Total smokers’)
In[10]:
#Population per region
print(‘Southwest patients’, count_data(‘southwest’,region))
print(‘Northwest patients’, count_data(‘northwest’,region))
print(‘Southeast patients’, count_data(‘southeast’,region))
print(‘Northeast patients’, count_data(‘northeast’,region))
In[11]:
average insrance costs
print(‘Average insurance costs’, average_number(charges))
In[12]:
Cost of insurance for a string category
def cost(category,category_list):
list_check =
list_check = list(zip(category_list,charges))
total_cost = 0
for patient in list_check:
if patient[0] == category:
total_cost += patient[1]
return round(total_cost)
print(‘Smokers insurance costs is’, cost(‘yes’, smoker))
print(’ vs non-smokers insurance costs’, cost(‘no’,smoker))
In[14]:
#Check average BMI and distribution
count_greater = 0
count_smaller = 0
for i in bmi:
if i < average_number(bmi):
count_smaller += 1
elif i > average_number(bmi):
count_greater += 1
print(‘Number of patients smaller than BMI average of’, average_number(bmi),‘is’,count_smaller)
print(‘Number of patients greater than BMI average of’, average_number(bmi),‘is’,count_greater)
In[18]:
#insurance cost as per BMI
lst_bmi_cost = list(zip(bmi,charges))
insurance_greater_bmi = 0
insurance_lower_bmi = 0
for i in lst_bmi_cost:
if i[0] < average_number(bmi):
insurance_lower_bmi += i[1]
elif i[0] > average_number(bmi):
insurance_greater_bmi += i[1]
print(‘Insurance costs for greater BMI than average’, average_number(bmi), 'is ',round(insurance_greater_bmi))
print('Insurance costs for lower BMI than average', average_number(bmi), 'is ',round(insurance_lower_bmi))
In[20]:
#insurance cost as children
lst_children_cost = list(zip(children,charges))
insurance_with_children = 0
insurance_with_nochildren = 0
for i in lst_children_cost:
if i[0] > 0:
insurance_with_children += i[1]
else:
insurance_with_nochildren += i[1]
print(‘Insurance with children’,round(insurance_with_children), ‘vs Insurance with no children’,round(insurance_with_nochildren))