# U.S. Insurance Cost amazing solution for data Analysis - Gie me your feedback

The U.S, Insurance Cost Code wa easy to type, but it was a great challenge for learning all the subjects we saw in the exercises. It took me about 3 hours to complete the code, including all the reasearch I had to do in google. The link to my code is:

from pdb import set_trace
import csv
import operator
name_of_file = “Insurance.csv”
def create_dictionaries(name_of_file):
list_of_data =

``````with open(name_of_file,"r") as csv_data:

for row in csv_dictionaries:
list_of_data.append(row)

return list_of_data
``````

csv_dictionaries = create_dictionaries(name_of_file)
print(csv_dictionaries)

def calculate_medium_age(csv_dictionaries):
age = 0
for i in csv_dictionaries:
age+=int(i[“age”])
medium_age = age/len(csv_dictionaries)
return medium_age

medium_age = calculate_medium_age(csv_dictionaries)
print(medium_age)

def calculate_male_female_perc(csv_dictionaries):
males = 0
females = 0
for i in csv_dictionaries:
if i[“sex”] == “female”:
females +=1
elif i[“sex”] == “male”:
males +=1
else:
continue
return [males,females]

[males,females] = calculate_male_female_perc(csv_dictionaries)
print("Males : " + str(males) + " And Females: " + str(females))

def numbers_by_region(csv_dictionaries):
northeast = 0
northwest = 0
southeast = 0
southwest = 0
for i in csv_dictionaries:
if i[“region”] == “southeast”:
southeast +=1
elif i[“region”] == “southwest”:
southwest+=1
elif i[“region”] == “northeast”:
northeast+=1
elif i[“region”] == “northwest”:
northwest+=1
else:
continue

``````temp = [southeast, southwest, northeast, northwest]
temp_regions = ["southeast", "southwest", "northeast", "northwest"]
temp_dict = { temp_regions[i]: temp[i] for i in range(len(temp))}
sorted_tuples = sorted(temp_dict.items(), key=operator.itemgetter(1))
sorted_dict = dict(sorted_tuples)
return sorted_dict
``````

sorted_dict = numbers_by_region(csv_dictionaries)
print(sorted_dict)

def calculate_medium_charges(csv_dictionaries):
charges = 0
for i in csv_dictionaries:
charges+= float(i[“charges”])
medium_charges = charges/len(csv_dictionaries)
return medium_charges

medium_charges = calculate_medium_charges(csv_dictionaries)
print(medium_charges)

def calculate_percentage_in_age(csv_dictionaries):
age20 = 0
age20_30 = 0
age30_40 = 0
age40_50 = 0
age50_60 = 0
age60plus = 0
for i in csv_dictionaries:
if int(i[“age”])<=20:
age20+=1
elif int(i[“age”])<=30:
age20_30+=1
elif int(i[“age”])<=40:
age30_40 +=1
elif int(i[“age”])<=50:
age40_50 +=1
elif int(i[“age”])<=60:
age50_60 +=1
else:
age60plus+=1
totalnumber= len(csv_dictionaries)
perage20 = round(100age20/totalnumber,2)
perage20_30 = round(100
age20_30/totalnumber,2)
perage30_40 = round(100age30_40/totalnumber,2)
perage40_50 = round(100
age40_50/totalnumber,2)
perage50_60 = round(100age50_60/totalnumber,2)
perage60plus = round(100
age60plus/totalnumber,2)
perc_dict = {“age20”:perage20,“age20_30”:perage20_30,“age30_40”:perage30_40,“age40_50”:perage40_50,“age50_60”:perage50_60,“age60plus”:perage60plus}
return perc_dict

percentage_age = calculate_percentage_in_age(csv_dictionaries)
print(percentage_age)

def medium_charges_by_age(csv_dictionaries):
chargesage20 = 0
chargesage20_30 = 0
chargesage30_40 = 0
chargesage40_50 = 0
chargesage50_60 = 0
chargesage60plus = 0
age20 = 0
age20_30 = 0
age30_40 = 0
age40_50 = 0
age50_60 = 0
age60plus = 0
for i in csv_dictionaries:
if int(i[“age”])<=20:
age20+=1
chargesage20+=float(i[“charges”])
elif int(i[“age”])<=30:
age20_30+=1
chargesage20_30+=float(i[“charges”])
elif int(i[“age”])<=40:
age30_40 +=1
chargesage30_40+=float(i[“charges”])
elif int(i[“age”])<=50:
age40_50 +=1
chargesage40_50+=float(i[“charges”])
elif int(i[“age”])<=60:
age50_60 +=1
chargesage50_60+=float(i[“charges”])
else:
age60plus+=1
chargesage60plus+=float(i[“charges”])

``````mediumchargesage20= chargesage20/age20
mediumchargesage20_30=chargesage20_30/age20_30
mediumchargesage30_40=chargesage30_40/age30_40
mediumchargesage40_50=chargesage40_50/age40_50
mediumchargesage50_60=chargesage50_60/age50_60
mediumchargesage60plus=chargesage60plus/age60plus

medium_charge_dict = {"mediumchargesage20":mediumchargesage20,"mediumchargesage20_30":mediumchargesage20_30," mediumchargesage30_40": mediumchargesage30_40,"mediumchargesage40_50":mediumchargesage40_50,"mediumchargesage50_60":mediumchargesage50_60,"mediumchargesage60plus":mediumchargesage60plus}
return medium_charge_dict
``````

medium_charge_dict = medium_charges_by_age(csv_dictionaries)
print(medium_charge_dict)