In [11]:
#Page 1
'''
Read the CSV file "US_births_1994-2003_CDC_NCHS.csv" into a string.
Split the string on the newline character ("\n").
Display the first 10 values in the resulting list.
'''
f = open("US_births_1994-2003_CDC_NCHS.csv", 'r')
data = f.read().split("\n")
data[0:10]


Out[11]:
['year,month,date_of_month,day_of_week,births',
 '1994,1,1,6,8096',
 '1994,1,2,7,7772',
 '1994,1,3,1,10142',
 '1994,1,4,2,11248',
 '1994,1,5,3,11053',
 '1994,1,6,4,11406',
 '1994,1,7,5,11251',
 '1994,1,8,6,8653',
 '1994,1,9,7,7910']

In [5]:
#Page 2
'''
Create a function named read_csv() that:
Takes a single, required argument, a string representing the file name of the CSV file.
Reads the file into a string, splits the string on the newline character ("\n"), and removes the header row. Assign this list to string_list and create an empty list named final_list.
Uses a for loop to:
Iterate over string_list,
Create an empty list named int_fields,
Splits each row on the comma delimiter (,) and assigns the resulting list to string_fields,
Converts each value in string_fields to an integer and appends to int_fields,
Appends int_fields to final_list.
Returns final_list.
Use the read_csv() function to read in the file "US_births_1994-2003_CDC_NCHS.csv" and assign the result to cdc_list.
Display the first 10 rows of cdc_list to confirm it's a list of lists, containing only integer values, and no header row.
'''
def read_csv(filename):
    f = open(filename, 'r')
    data = f.read().split("\n")
    string_list  = data[1:len(data)]
    final_list =[]
    
    for s in string_list:
        int_fields = []
        string_fields = s.split(",")
        for f in string_fields:
            int_fields.append(int(f))
        final_list.append(int_fields)
    return final_list
cdc_list = read_csv("US_births_1994-2003_CDC_NCHS.csv")
print(cdc_list[0:10])


[[1994, 1, 1, 6, 8096], [1994, 1, 2, 7, 7772], [1994, 1, 3, 1, 10142], [1994, 1, 4, 2, 11248], [1994, 1, 5, 3, 11053], [1994, 1, 6, 4, 11406], [1994, 1, 7, 5, 11251], [1994, 1, 8, 6, 8653], [1994, 1, 9, 7, 7910], [1994, 1, 10, 1, 10498]]

In [9]:
#Page 3
'''
Create a function named month_births() that:
Takes a single, required argument, a list of lists.
Creates an empty dictionary, births_per_month, to store the monthly totals.
Uses a for loop to:
Iterate over the list of lists,
Extract the value in the month and births columns,
If the month value already exists as a key in births_per_month, the births value is added to the existing value,
If the month value doesn't exist as a key in births_per_month, it's created and the associated value is the births value.
After the loop, return the births_per_month dictionary.
Use the month_births() function to calculate the monthly totals for the dataset and assign the result to cdc_month_births. Display the dictionary.
'''
def month_births(list_Of_list):
    births_per_month = {} 
    for list in list_Of_list:
        month = list[1]
        births = list[4]
        if month in births_per_month:
            births_per_month[month] += births
        else:
            births_per_month[month] = births
    return births_per_month
cdc_month_births = month_births(cdc_list)
print(cdc_month_births)


{1: 3232517, 2: 3018140, 3: 3322069, 4: 3185314, 5: 3350907, 6: 3296530, 7: 3498783, 8: 3525858, 9: 3439698, 10: 3378814, 11: 3171647, 12: 3301860}

In [10]:
#Page 4
'''
Create a function named dow_births() that takes a single, required argument (a list of lists) and returns a dictionary containing the total number of births for each unique value of the day_of_week column.
Use the dow_births() function to return the day-of-week totals for the dataset and assign the result to cdc_day_births. Display the dictionary.
'''
def dow_births(list_Of_list):
    births_per_week = {}
    for list in list_Of_list:
        day_of_week = list[3]
        births = list[4]
        if day_of_week in births_per_week:
            births_per_week[day_of_week] += births
        else:
            births_per_week[day_of_week] = births
    return births_per_week
cdc_day_births = dow_births(cdc_list)
print(cdc_day_births)


{1: 5789166, 2: 6446196, 3: 6322855, 4: 6288429, 5: 6233657, 6: 4562111, 7: 4079723}

In [13]:
#Page 4
'''
Create a function named calc_counts() that:
Takes two, required parameters:
data: a list of lists
column: the column number we want to calculate the totals for
Populates and returns a dictionary containing the total number of births for each unique value in the column at position column.
Use the calc_counts() function to:
Return the yearly totals for the dataset and assign the result to cdc_year_births.
Return the monthly totals for the dataset and assign the result to cdc_month_births.
Return the day-of-month totals for the dataset and assign the result to cdc_dom_births.
Return the day-of-week totals for the dataset and assign the result to cdc_dow_births.
'''
def calc_counts(data, column):
    births_by_column = {}
    for list in data:
        col_val = list[column]
        births = list[4]
        if col_val in births_by_column:
            births_by_column[col_val] += births
        else:
            births_by_column[col_val] = births
    return births_by_column
cdc_year_births = calc_counts(cdc_list, 0)
cdc_month_births = calc_counts(cdc_list, 1)
cdc_dom_births = calc_counts(cdc_list, 2)
cdc_dow_births = calc_counts(cdc_list, 3)

print(cdc_year_births)
print("\n")
print(cdc_month_births)
print("\n")
print(cdc_dom_births)
print("\n")
print(cdc_dow_births)


{2000: 4058814, 2001: 4025933, 2002: 4021726, 2003: 4089950, 1994: 3952767, 1995: 3899589, 1996: 3891494, 1997: 3880894, 1998: 3941553, 1999: 3959417}


{1: 3232517, 2: 3018140, 3: 3322069, 4: 3185314, 5: 3350907, 6: 3296530, 7: 3498783, 8: 3525858, 9: 3439698, 10: 3378814, 11: 3171647, 12: 3301860}


{1: 1276557, 2: 1288739, 3: 1304499, 4: 1288154, 5: 1299953, 6: 1304474, 7: 1310459, 8: 1312297, 9: 1303292, 10: 1320764, 11: 1314361, 12: 1318437, 13: 1277684, 14: 1320153, 15: 1319171, 16: 1315192, 17: 1324953, 18: 1326855, 19: 1318727, 20: 1324821, 21: 1322897, 22: 1317381, 23: 1293290, 24: 1288083, 25: 1272116, 26: 1284796, 27: 1294395, 28: 1307685, 29: 1223161, 30: 1202095, 31: 746696}


{1: 5789166, 2: 6446196, 3: 6322855, 4: 6288429, 5: 6233657, 6: 4562111, 7: 4079723}

In [ ]: