Dictionaries - dictionary only showing one line

Hi,

I am trying to create a patient info dictionary.
However, my code is only creating a dictionary containing one individual’s medical info.
I am not quite sure how to get it to iterate through all of the data. My suspicion is that it needs to iterate through each line of data in the csv, but I am not quite sure on how to do that…

The data I am reading presents itself with the following format:
[‘42’, ‘male’, ‘31.255’, ‘0’, ‘no’, ‘northwest’, ‘6358.77645’]
[‘25’, ‘male’, ‘29.7’, ‘3’, ‘yes’, ‘southwest’, ‘19933.458’]
[‘57’, ‘male’, ‘18.335’, ‘0’, ‘no’, ‘northeast’, ‘11534.87265’]

def create_patient_info_dict(csv_file): 
    with open(csv_file) as csv_data: # opens the csv without DictReader
        csv_data_reader = csv.reader(csv_data)
        
        patient_info_dict = {}
        
        for info in csv_data_reader:
            patient_info_dict = {"Age": info[0],
                                 "Sex": info[1],
                                 "BMI": info[2],
                                 "Number of Children": info[3],
                                 "Smoker Status": info[4],
                                 "Region": info[5],
                                 "Insurance Charge": info[6]}
        return patient_info_dict

patient_info = create_patient_info_dict("insurance.csv")
        
print(patient_info)

Thank you very much for your help!

I feel like what’s happening in this line is that you are iterating but setting the same 1 item in the dictionary to change in every iteration here:

for info in csv_data_reader:
            patient_info_dict = {"Age": info[0],
                                 "Sex": info[1],
                                 "BMI": info[2],
                                 "Number of Children": info[3],
                                 "Smoker Status": info[4],
                                 "Region": info[5],
                                 "Insurance Charge": info[6]}

What you can do is have a wrapper that’s either an index-ID number or the name if you have that. If it’s an index number, I see no reason why you can’t make a list of dictionaries (instead of a dictionary of dictionaries).

List version might look something like this (but of course, dictionary of dictionaries might be the way to go, it really depends on context).

patient_info_dict = []
for info in csv_data_reader:
       patient_info_dict.append({"Age": info[0],
                                 "Sex": info[1],
                                 "BMI": info[2],
                                 "Number of Children": info[3],
                                 "Smoker Status": info[4],
                                 "Region": info[5],
                                 "Insurance Charge": info[6]})

For dictionary of dictionaries, if you don’t have a name to tie it to, you can use csv_data_reader.index(info) as a counter (although patient id would be better, and would mark a clear improvement over using a list).

patient_info_dict = {}
for info in csv_data_reader:
       patient_info_dict[csv_data_reader.index(info)] = {"Age": info[0],
                                 "Sex": info[1],
                                 "BMI": info[2],
                                 "Number of Children": info[3],
                                 "Smoker Status": info[4],
                                 "Region": info[5],
                                 "Insurance Charge": info[6]}

or something like that. There may be a better way, these are just some ideas…

2 Likes