One of the initial parts of the U.S. Medical Insurance Costs Project is to import the dataset in order to make the analysis. To do so, I imported the CSV file and my initial goal was to create n lists for each header in the dataset to get all values of each variable. However, when I try to do this iterating for each header and row in the CSV file, I do not get the expected result: only the content of the first header (“column”) is iterated- the other ones are not.
For example: when I run the code below, only the values of the first header (“age”) are printed. The values of the other 6 headers are not.
with open("insurance.csv", newline = "") as insurance_data: dataset = csv.DictReader(insurance_data, delimiter = ",") headers = dataset.fieldnames for header in headers: print(header) for row in dataset: print(row[header])
The beginning of the output is, as expected:
age 19 18 28 [...]
But the end is:
[...] 21 61 sex bmi children smoker region charges
An easy solution to that would be adding lists manually for each header in the dataset. Although this is okay with a dataset with only 7 columns, this can be quite tiring for a dataset with more variables - and that’s why I’m avoiding this strategy.
Does anyone know who I can iterate through this sequence of dictionaries in order to create a list for each variable?