Hey there, I liked what type of questions you were trying to answer and that you were able to calculate statistics to figure out some of those questions.
I did have some tips that might help with your code:
I noticed you made a dictionary of all the patients. The initial part of the code with the DictReader
creates a dictionary of all the entries in the csv file so in order to use it you can .append
it to a new list like so:
import csv
with open('insurance.csv') as insurance_csv:
insurance_reader = csv.DictReader(insurance_csv)
new_list = []
for row in insurance_reader:
new_list.append(row)
print(new_list[0:4])
the last line prints the first 5 dictionaries in the list which look like this:
[{'age': '19', 'sex': 'female', 'bmi': '27.9', 'children': '0', 'smoker': 'yes', 'region': 'southwest', 'charges': '16884.924'}, {'age': '18', 'sex': 'male', 'bmi': '33.77', 'children': '1', 'smoker': 'no', 'region': 'southeast', 'charges': '1725.5523'}, {'age': '28', 'sex': 'male', 'bmi': '33', 'children': '3', 'smoker': 'no', 'region': 'southeast', 'charges': '4449.462'}, {'age': '33', 'sex': 'male', 'bmi': '22.705', 'children': '0', 'smoker': 'no', 'region': 'northwest', 'charges': '21984.47061'}]
I also noticed in the area where you calculate average BMI for each sex that you have a bunch of code in different cells. Calculating an average is a great opportunity to bundle that up into a function which could take in a list as a parameter and execute all your code to get an average. For example something like this:
def average_for_sexes(info_list):
new_list = zip(sex_list, info_list)
total_bmi_m = 0
total_bmi_w = 0
total_m = 0
total_w = 0
for person in new_list:
if person[0] == 'female':
etc.....
I also think that instead of creating 4 variables to keep track of numbers, it might be easier to create 2 lists instead. You could append each number to a “male” and a “female” list then get the average by doing sum and divide by length of list. Here is an example:
def average_for_sexes(info_list, trait):
new_list = zip(sex_list, info_list)
female_list = []
male_list = []
for person in new_list:
if person[0] == 'female':
female_list.append(person[1])
else:
male_list.append(person[1])
avg_female = round(sum(female_list)/len(female_list),2)
avg_male = round(sum(male_list)/len(male_list),2)
print(
"""The average female {trait} is {}
compared to the average male {trait}
of {}""".format(avg_female, avg_male)
Great job, and good luck on your coding journey!