First, we are going to build the pieces that go into the function. Once we're done, we're going to write the function itself. For testing, we choose BALTIMORE as the city and 3 as the biweek.
In [1]:
city = 'BALTIMORE'
In [2]:
biweek = 3
Open the file using the csv package:
In [3]:
import csv
with open('../data/Dalziel2015_data.csv') as csvfile:
reader = csv.DictReader(csvfile)
print(next(reader))
As you can see, this reads each row in the file an converts it into a dictionary, using the header to name the keys. This makes it very simple to filter the desired records:
In [4]:
with open('../data/Dalziel2015_data.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['loc'] == city and int(row['biweek']) == biweek:
print(row['year'], row['cases'])
Now that we have the code ready, we can write a function:
In [5]:
import csv
def get_cases_city_biweek(city = "BALTIMORE", biweek = 5):
with open('../data/Dalziel2015_data.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['loc'] == city and int(row['biweek']) == biweek:
print(row['year'], row['cases'])
We can call the function using another city/biweek, for example BOSTON and 7:
In [6]:
get_cases_city_biweek('BOSTON', 7)
There are different ways to do this. Here, the strategy is to construct a set, so that cities are not repeated:
In [7]:
def get_all_cities():
all_cities = set([])
with open('../data/Dalziel2015_data.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
all_cities.add(row['loc'])
return(all_cities)
Now invoke the function:
In [8]:
get_all_cities()
Out[8]:
In [ ]: