-
-
Save sydneykay514/57a00d6dce2145177e37cb164bc1727c to your computer and use it in GitHub Desktop.
unfinished/ errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('insurance.csv') as file: | |
file_dict = csv.DictReader(file) | |
def mean_charges(dictionary): | |
age_charges = {'Infant': 0, 'Child': 0, 'Adolescent': 0, 'Early Adult': 0, 'Adult': 0, 'Middle Age': 0, | |
'Early Elder': 0, 'Late Elder': 0} | |
sex_charges = {'Male': 0, 'Female': 0} | |
bmi_charges = {'Severely Underweight': 0, 'Underweight': 0, 'Normal Weight': 0, 'Overweight': 0, | |
'Obesity Class I': 0, | |
'Obesity Class II': 0, 'Obesity Class III': 0} | |
children_charges = {} | |
smoker_charges = {} | |
region_charges = {} | |
charges_mean = {} | |
i_total = 0 | |
i_count = 0 | |
c_total = 0 | |
c_count = 0 | |
ado_total = 0 | |
ado_count = 0 | |
ea_total = 0 | |
ea_count = 0 | |
a_total = 0 | |
a_count = 0 | |
ma_total = 0 | |
ma_count = 0 | |
ee_total = 0 | |
ee_count = 0 | |
le_total = 0 | |
le_count = 0 | |
male_count = 0 | |
male_total = 0 | |
female_count = 0 | |
female_total = 0 | |
su_total = 0 | |
su_count = 0 | |
u_total = 0 | |
u_count = 0 | |
nw_total = 0 | |
nw_count = 0 | |
o_total = 0 | |
o_count = 0 | |
oc1_total = 0 | |
oc1_count = 0 | |
oc2_total = 0 | |
oc2_count = 0 | |
oc3_total = 0 | |
oc3_count = 0 | |
for row in dictionary: | |
current_age = float(row['age']) | |
current_charges = float(row['charges']) | |
if current_age < 2: | |
i_total += current_charges | |
i_count += 1 | |
elif current_age <= 9: | |
c_total += current_charges | |
c_count += 1 | |
elif current_age <= 19: | |
ado_total += current_charges | |
ado_count += 1 | |
elif current_age <= 29: | |
ea_total += current_charges | |
ea_count += 1 | |
elif current_age <= 39: | |
a_total += current_charges | |
a_count += 1 | |
elif current_age <= 59: | |
ma_total += current_charges | |
ma_count += 1 | |
elif current_age <= 80: | |
ee_total += current_charges | |
ee_count += 1 | |
elif current_age > 80: | |
le_total += current_charges | |
le_count += 1 | |
if i_count != 0: | |
age_charges['Infant'] = i_total / i_count | |
if c_count != 0: | |
age_charges['Child'] = c_total / c_count | |
if ado_count != 0: | |
age_charges['Adolescent'] = ado_total / ado_count | |
if ea_count != 0: | |
age_charges['Early Adult'] = ea_total / ea_count | |
if a_count != 0: | |
age_charges['Adult'] = a_total / a_count | |
if ma_count != 0: | |
age_charges['Middle Age'] = ma_total / ma_count | |
if ee_count != 0: | |
age_charges['Early Elder'] = ee_total / ee_count | |
if le_count != 0: | |
age_charges['Late Elder'] = le_total / le_count | |
for row in dictionary: | |
current_sex = row['sex'] | |
current_charges = float(row['charges']) | |
if current_sex == 'male': | |
male_total += current_charges | |
male_count += 1 | |
elif current_sex == 'female': | |
female_total += current_charges | |
female_count += 1 | |
sex_charges['Male'] = male_total / male_count | |
sex_charges['Female'] = female_total / female_count | |
for row in dictionary: | |
current_bmi = float(row['bmi']) | |
current_charges = float(row['charges']) | |
if current_bmi < 16.5: | |
su_total += current_charges | |
su_count += 1 | |
elif current_bmi < 18.5: | |
u_total += current_charges | |
u_count += 1 | |
elif current_bmi <= 24.9: | |
nw_total += current_charges | |
nw_count += 1 | |
elif current_bmi <= 29.9: | |
o_total += current_charges | |
o_count += 1 | |
elif current_bmi <= 34.9: | |
oc1_total += current_charges | |
oc1_count += 1 | |
elif current_bmi <= 39.9: | |
oc2_total += current_charges | |
oc2_count += 1 | |
elif current_bmi > 39.9: | |
oc3_total += current_charges | |
oc3_count += 1 | |
bmi_charges['Severely Underweight'] = su_total / su_count | |
bmi_charges['Underweight'] = u_total / u_count | |
bmi_charges['Normal Weight'] = nw_total / nw_count | |
bmi_charges['Overweight'] = o_total / o_count | |
bmi_charges['Obesity Class I'] = oc1_total / oc1_count | |
bmi_charges['Obesity Class II'] = oc2_total / oc2_count | |
bmi_charges['Obesity Class III'] = oc3_total / oc3_count | |
return age_charges, sex_charges, bmi_charges | |
print(mean_charges(file_dict)) | |
def get_counts(dictionary): | |
counts = {} | |
regions = {} | |
sexes = {} | |
children = {} | |
children_sorted = {} | |
smokers = {} | |
bmi = {'Severely Underweight': 0, 'Underweight': 0, 'Normal Weight': 0, 'Overweight': 0, 'Obesity Class I': 0, | |
'Obesity Class II': 0, 'Obesity Class III': 0} | |
ages = {'Infant': 0, 'Child': 0, 'Adolescent': 0, 'Early Adult': 0, 'Adult': 0, 'Middle Age': 0, | |
'Early Elder': 0, 'Late Elder': 0} | |
for row in dictionary: | |
current_region = row['region'] | |
current_sex = row['sex'] | |
current_children = row['children'] | |
current_smoker = row['smoker'] | |
current_bmi = float(row['bmi']) | |
current_age = float(row['age']) | |
if current_region not in regions: | |
regions[current_region] = 1 | |
else: | |
regions[current_region] += 1 | |
if current_sex not in sexes: | |
sexes[current_sex] = 1 | |
else: | |
sexes[current_sex] += 1 | |
if current_children not in children: | |
children[current_children] = 1 | |
else: | |
children[current_children] += 1 | |
if current_smoker not in smokers: | |
smokers[current_smoker] = 1 | |
else: | |
smokers[current_smoker] += 1 | |
if current_bmi < 16.5: | |
bmi['Severely Underweight'] += 1 | |
elif current_bmi < 18.5: | |
bmi['Underweight'] += 1 | |
elif current_bmi <= 24.9: | |
bmi['Normal Weight'] += 1 | |
elif current_bmi <= 29.9: | |
bmi['Overweight'] += 1 | |
elif current_bmi <= 34.9: | |
bmi['Obesity Class I'] += 1 | |
elif current_bmi <= 39.9: | |
bmi['Obesity Class II'] += 1 | |
elif current_bmi > 39.9: | |
bmi['Obesity Class III'] += 1 | |
if current_age < 2: | |
ages['Infant'] += 1 | |
elif current_age <= 9: | |
ages['Child'] += 1 | |
elif current_age <= 19: | |
ages['Adolescent'] += 1 | |
elif current_age <= 29: | |
ages['Early Adult'] += 1 | |
elif current_age <= 39: | |
ages['Adult'] += 1 | |
elif current_age <= 59: | |
ages['Middle Age'] += 1 | |
elif current_age <= 80: | |
ages['Early Elder'] += 1 | |
elif current_age > 80: | |
ages['Late Elder'] += 1 | |
for i in sorted(children): | |
children_sorted[i] = children[i] | |
counts['Region'] = regions | |
counts['Sex'] = sexes | |
counts['Number of Children'] = children_sorted | |
counts['Smoker Status'] = smokers | |
counts['BMI Classifications'] = bmi | |
counts['Age Demographics'] = ages | |
return counts | |
counts = get_counts(file_dict) | |
print(counts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment