Skip to content

Instantly share code, notes, and snippets.

@baskaufs
Created September 19, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baskaufs/d7a85bbe23b39b3276b5b888c5493ad8 to your computer and use it in GitHub Desktop.
Save baskaufs/d7a85bbe23b39b3276b5b888c5493ad8 to your computer and use it in GitHub Desktop.
Replaces empty strings in a CSV with zeros
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