Created
September 19, 2020 13:43
-
-
Save baskaufs/d7a85bbe23b39b3276b5b888c5493ad8 to your computer and use it in GitHub Desktop.
Replaces empty strings in a CSV with zeros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from statistics import mean | |
# read from a CSV file into a list of dictionaries | |
def read_dict(filename): | |
with open(filename, 'r', newline='', encoding='utf-8') as file_object: | |
dict_object = csv.DictReader(file_object) | |
array = [] | |
for row in dict_object: | |
array.append(row) | |
return array | |
filename = 'Metro_Nashville_Schools.csv' | |
schools_data = read_dict(filename) | |
asian = [] | |
for school in schools_data: | |
if school['Asian'] == '': | |
asian.append(0) | |
else: | |
asian.append(int(school['Asian'])) | |
mean(asian) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment