<jupyter_start><jupyter_text>Python Reading and Writing CSV files<NAME>, PhD6/26/2018 Import the datafile mpg.csv, which contains fuel economy data for 234 cars.* mpg : miles per gallon* class : car classification* cty : city mpg* cyl :  of cylinders* displ : engine displacement in liters* drv : f = front-wheel drive, r = rear wheel drive, 4 = 4wd* fl : fuel (e = ethanol E85, d = diesel, r = regular, p = premium, c = CNG)* hwy : highway mpg* manufacturer : automobile manufacturer* model : model of car* trans : type of transmission* year : model year<jupyter_code>import csv

%precision 2

with open('data/mpg.csv') as csvfile:
    mpg = list(csv.DictReader(csvfile))
    
mpg[:3] # The first three dictionaries in our list.<jupyter_output><empty_output><jupyter_text>`csv.Dictreader` has read in each row of the csv file as a dictionary. `len` shows that our list is comprised of 234 dictionaries.<jupyter_code>len(mpg)<jupyter_output><empty_output><jupyter_text>`keys` gives us the column names of our csv.<jupyter_code>mpg[0].keys()<jupyter_output><empty_output><jupyter_text>Find the average cty fuel economy across all cars. All values in the dictionaries are strings, so we need to convert to float.<jupyter_code>[float(d['cty']) for d in mpg]
sum(float(d['cty']) for d in mpg) / len(mpg)<jupyter_output><empty_output><jupyter_text>Find the average hwy fuel economy across all cars.<jupyter_code>sum(float(d['hwy']) for d in mpg) / len(mpg)<jupyter_output><empty_output><jupyter_text>Use `set` to return the unique values for the number of cylinders the cars in our dataset have.<jupyter_code>cylinders = set(d['cyl'] for d in mpg)
cylinders<jupyter_output><empty_output><jupyter_text>Group the cars by number of cylinder, and finding the average cty mpg for each group.<jupyter_code>CtyMpgByCyl = []

for c in cylinders: # iterate over all the cylinder levels
    summpg = 0
    cyltypecount = 0
    for d in mpg: # iterate over all dictionaries
        if d['cyl'] == c: # if the cylinder level type matches,
            summpg += float(d['cty']) # add the cty mpg
            cyltypecount += 1 # increment the count
    CtyMpgByCyl.append((c, summpg / cyltypecount)) # append the tuple ('cylinder', 'avg mpg')

CtyMpgByCyl.sort(key=lambda x: x[0])
CtyMpgByCyl<jupyter_output><empty_output><jupyter_text>Use `set` to return the unique values for the class types in our dataset.<jupyter_code>vehicleclass = set(d['class'] for d in mpg) # what are the class types
vehicleclass<jupyter_output><empty_output><jupyter_text>Find the average hwy mpg for each class of vehicle in our dataset.<jupyter_code>HwyMpgByClass = []

for t in vehicleclass: # iterate over all the vehicle classes
    summpg = 0
    vclasscount = 0
    for d in mpg: # iterate over all dictionaries
        if d['class'] == t: # if the cylinder amount type matches,
            summpg += float(d['hwy']) # add the hwy mpg
            vclasscount += 1 # increment the count
    HwyMpgByClass.append((t, summpg / vclasscount)) # append the tuple ('class', 'avg mpg')

HwyMpgByClass.sort(key=lambda x: x[1])
HwyMpgByClass<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10265430124720179, 'fraction_numerical': 0.008954269267668692, 'mean_word_length': 4.697632058287796, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '432670', 'license': 'MIT', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1030, 'n_words': 353, 'path': 'notebooks/7 - Python Reading and Writing CSV files.ipynb', 'repo_name': 'larsonma/DataScienceIntro', 'size': '3131'}