In this challenge, you'll practice using modules, classes, and list comprehensions to process and represent a data set in Python. You'll be working with data on NFL player suspensions.
-
Read the dataset into a list of lists.
- Import the
csv
module. - Create a File handler for
nfl_suspensions_data.csv
. - Use the
csv.reader()
andlist()
methods to read the file into a list namednfl_suspensions
.
- Import the
-
Remove the first list in
nfl_suspensions
, which contains the header row of the CSV file.- Select all lists in
nfl_suspensions
, except the for the one at index 0. - Assign the resulting list of lists back to the variable
nfl_suspensions
.
- Select all lists in
-
Count the number of times each value in the
year
column occurs.- Create an empty dictionary called years.
- Use a
for
loop to iterate over the list innfl_suspensions
representing theyear
column:- Extract that row's value for the
year
column and assign it torow_year
. - If
row_year
is already a key inyears
, add 1 to the value for that key. - If
row_year
isn't already a key inyears
, set the value for the key to 1.
- Extract that row's value for the
Use the print() function to display the dictionary
years
.
import csv
nfl_suspensions = list (csv.reader(open('nfl_suspensions_data.csv','r')))
nfl_suspensions = nfl_suspensions[1:]
years ={}
for row in nfl_suspensions:
year = row[5]
if year in years:
years[year]+=1
else:
years[year] =1
print(years)
Let's explore the values in these columns by using sets and list comprehensions.
- Retrieve the unique values in the
team
column and assign the list tounique_teams
.- Use a list comprehension to create a new list containined just the values in the
team
column. - Use the
set()
function to return a list containing only the unique values and assign tounique_teams
.
- Use a list comprehension to create a new list containined just the values in the
-
Retrieve the unique values in the
games
column and assign the list tounique_games
.- Use a list comprehension to create a new list containined just the values in the
games
column. - Use the
set()
function to return a list containing only the unique values and assign tounique_games
.
- Use a list comprehension to create a new list containined just the values in the
Display
unique_teams
andunique_games
.
teams = [row[1] for row in nfl_suspensions]
unique_teams = set(teams)
unique_games = set([row[2] for row in nfl_suspensions])
print (unique_teams)
print (unique_games)
-
Create the
Suspension
class.- Define the
__init__()
method with the following criteria:- The sole required parameter is a list representing a row from the dataset.
- To create a
Suspension
instance, we want to be able to pass in a list fromnfl_suspensions
.
- To create a
- Currently, we're only interested in storing the
name
,team
,games
andyear
columns. Upon instantiation:- Set the name value for that row to the
name
property. - Set the team value for that row to the
team
property. - Set the games value for that row to the
games
property. - Set the year value for that row to the
year
property.
- Set the name value for that row to the
- The sole required parameter is a list representing a row from the dataset.
- Define the
Create a
Suspension
instance using the third row innfl_suspensions
, and assign it to the variablethird_suspension
.
class Suspension:
def __init__(self,row):
self.name = row[0]
self.team = row[1]
self.games= row[2]
self.year = row[3]
third_suspension = Suspension(nfl_suspensions[2])
print(third_suspension)
print(third_suspension.team)
print(third_suspension.name)
Let's tweak the Suspension
class a bit to extend its functionality. Right now, the value for year
is a string, rather than an integer. Let's modify the Suspension
class so that it stores the values as integers.
- Instead of assigning the value at index 5 to the
year
property directly, use a try except block that:- Tries to cast the value at index 5 to an integer
- If an exception is thrown, assign the value 0 to the
year
property instead
- Create a method
called get_year()
that returns the yearvalue
for thatSuspension
instance. - Create a
Suspension
instance using the 23rd row, and assign it tomissing_year
. - Use the
get_year() method
to assign the year of themissing_year
suspension instance totwenty_third_year
.
class Suspension:
def __init__(self,row):
self.name = row[0]
self.team = row[1]
self.games= row[2]
try :
self.year =int(row[5])
except Exception:
self.year = 0
def get_year(self):
return self.year
missing_year = Suspension(nfl_suspensions[22])
twenty_third_year = missing_year.get_year()
twenty_third_year