Friday, December 16, 2016

Practicing NFL Suspension dataset - IPython Notebook



In [2]:
import csv
In [11]:
#reading the file content
f = open("nfl-suspensions-data.csv")
nfl_suspensions = list(csv.reader(f))
In [13]:
print(nfl_suspensions[0])
['name', 'team', 'games', 'category', 'desc.', 'year', 'source']
In [14]:
nfl_suspensions.pop(0)
Out[14]:
['name', 'team', 'games', 'category', 'desc.', 'year', 'source']
In [15]:
print(nfl_suspensions[0])
['F. Davis', 'WAS', 'Indef.', 'Substance abuse, repeated offense', 'Marijuana-related', '2014', 'http://www.cbssports.com/nfl/eye-on-football/24448694/redskins-te-fred-davis-suspended-Indefiniteinitely-by-nfl']
In [16]:
years = {}
for val in nfl_suspensions:
    y = val[-2]
    if y in years:
        year_count = years[y]
        years[y] = year_count+1
    else:
        years[y] = 1
print(years)
{'1995': 1, '2011': 13, '2000': 1, '2005': 8, '2002': 7, '1946': 1, '1983': 1, '2010': 21, '1989': 17, '1963': 1, '2014': 29, '1999': 5, '1990': 3, '   ': 1, '1994': 1, '2009': 10, '2008': 10, '1947': 1, '2001': 3, '2012': 45, '1993': 1, '2004': 6, '2003': 9, '1986': 1, '2007': 17, '1997': 3, '1998': 2, '2006': 11, '2013': 40}
In [20]:
# unique teams
unique_teams = []
for val in nfl_suspensions:
    unique_teams.append(val[1])
unique_teams = set(unique_teams)
In [21]:
#unique games
unique_games = []
for val in nfl_suspensions:
    unique_games.append(val[2])
unique_games = set(unique_games)
In [22]:
print(unique_teams)
print(unique_games)
{'KC', 'MIA', 'CAR', 'MIN', 'BAL', 'SEA', 'WAS', 'SF', 'IND', 'ARI', 'LA', 'TB', 'TEN', 'CLE', 'STL', 'FREE', 'NE', 'NO', 'CIN', 'PIT', 'NYJ', 'SD', 'NYG', 'HOU', 'CHI', 'JAX', 'GB', 'DEN', 'PHI', 'DET', 'DAL', 'OAK', 'ATL', 'BUF'}
{'10', '2', 'Indef.', '14', '6', '5', '20', '8', '32', '3', '36', '1', '4', '16'}
In [49]:
# creating a class suspension to store the suspension details

class Suspension(object):
    def __init__(self,data):
        self.name = data[0]
        self.team = data[1]
        self.games = data[2]
        try:
            year = int(data[-2])
            self.year = year
        except Exception:
            self.year = 0
            
    def get_year(self):
        return self.year
        
    def __str__(self):
        return self.name +" " +str(self.year)
        
In [50]:
third_suspension = Suspension(nfl_suspensions[2])
In [51]:
print(third_suspension)
L. Brazill 2014
In [52]:
missing_year = Suspension(nfl_suspensions[22])
In [53]:
print(missing_year)
P. Hornung 0
In [55]:
print(missing_year.get_year())
0
 

No comments :

Post a Comment