Please offer any suggestions is this project portfolio ready?
import csv
Ages =
Sex =
BMI =
Num_children =
Smoker =
Region =
cost =
with open (‘insurance.csv’,‘r’) as insurance:
reader = csv.DictReader(insurance)
header = next(reader)
for row in reader:
Ages.append(int(row[‘age’]))
Sex.append(row[‘sex’])
BMI.append(row[‘bmi’])
Num_children.append(row[‘children’])
Smoker.append(row[‘smoker’])
Region.append(row[‘region’])
cost.append(float(row[‘charges’]))
print(cost)
In[35]:
cost_by_smoker_region = {}
for i in range(len(Smoker)):
smoker = Smoker[i]
region = Region[i]
cost_i = cost[i]
if (smoker, region) not in cost_by_smoker_region:
cost_by_smoker_region[(smoker, region)] =
cost_by_smoker_region[(smoker, region)].append(cost_i)
average_smoker = 0
smoker_count = 0
average_non_smoker = 0
non_smoker_count = 0
for (smoker, region), costs in cost_by_smoker_region.items():
avg_cost = sum(costs) / len(costs)
if smoker == 'yes':
average_smoker += avg_cost
smoker_count += 1
print(f'The average cost for a smoker in {region} is {avg_cost}')
if smoker == 'no':
average_non_smoker += avg_cost
non_smoker_count += 1
print(f'The average cost for a non-smoker in {region} is {avg_cost}')
smoker_cost = average_smoker/smoker_count
print(f’The total average cost for a smoker is {smoker_cost}‘)
non_smoker_cost = average_non_smoker/non_smoker_count
print(f’The total average cost for a non-smoker is {non_smoker_cost}’)
In[28]:
#average age
def average_age(Ages):
return sum(Ages)/len(Ages)
result = average_age(Ages)
print(result)
In[33]:
#sex Count
Male = Sex.count(“male”)
female = Sex.count(“female”)
print(Male,female)
In[44]:
def count_age_sex(Ages,Sex):
age_group_count ={}
for i in range(len(Ages)):
age = Ages[i]
sex = Sex[i]
if age not in age_group_count:
age_group_count[age] = {'Male': 0, 'Female': 0}
# Increment the count for the current sex
if sex == 'Male':
age_group_count[age]['Male'] += 1
else:
age_group_count[age]['Female'] += 1
return age_group_count