Is there a way to tell csv.DictReader to pass certain columns as int/float?

Hi guys,

I was working on the US Insurance Portfolio Project (Lesson 6 in the DS Path), and I started experimenting with csv.DictReader. One thing I noted tho is that if I create a dictionary straight from it the way we were told in the lessons prior this one, I get a dictionary were all variables passed on from my csv are strings, while in fact many of them are needed as either float or int for analysis purposes.

Basically, using

patient_ids = list(range(0,1338))
with open('insurance.csv') as insurance_data:
    temp_dict = csv.DictReader(insurance_data)
    insurance_dict = dict(zip(patient_ids, temp_dict))
print(insurance_dict[0])

Returns the entry for the first patient with all their data as strings:
{'Age': '19', 'Sex': 'female', 'BMI': '27.9', 'Number of Children': '0', 'Smoker': 'yes', 'Region': 'southwest', 'Charges': '16884.924'}

The only way I got to make it work the way I wanted it – a dictionary with each patient as key and each variable in the values with the correct format – was to use the following:

patient_ids = list(range(0,1338))
ages = []
sexes = []
bmis = []
number_children = []
smoker_status = []
regions = []
total_charges = []

with open('insurance.csv') as insurance_data:
    temp_dict = csv.DictReader(insurance_data)
    for row in temp_dict:
        ages.append(int(row['age']))
        sexes.append(row['sex'])
        bmis.append(float(row['bmi']))
        number_children.append(int(row['children']))
        smoker_status.append(row['smoker'])
        regions.append(row['region'])
        total_charges.append(float(row['charges']))
    
insurance_dict = {}
for i in patient_ids:
    insurance_dict.update({patient_ids[i]: {"Age": ages[i], "Sex": sexes[i], "BMI": bmis[i], "Number of Children": number_children[i], "Smoker": smoker_status[i], "Region": regions[i], "Charges": total_charges[i]}})

print(insurance_dict[0])

###Returns {'Age': 19, 'Sex': 'female', 'BMI': 27.9, 'Number of Children': 0, 'Smoker': 'yes', 'Region': 'southwest', 'Charges': 16884.924}

Which, although effective, is not very elegant. So I was wondering, is there a way to pass arguments to DictReader to tell it what columns in each row are int or float? It strikes me as something so critical for CSV analyses that I would be surprised if the method lacked this ability.

Best regards,

Seb