def runs(T, D): ''' compute the expected runs created by a team based upon Bill James' runs created formula. You need to first compute the total number of hits (H) in the team, and total number of second base (_2B) in the team, etc. Then you could use Bill James' runs created formula to compute the expected runs created by the team. Input: T: a python list of playerID's, for example, ['zitoba01', 'hiljuer01', ...] D: a data frame loaded from 'Batting2001AJS.csv', which we processed in problem4.py. Output: RC: the expected runs created/scored by a team, a float scalar. ''' ######################################### ## INSERT YOUR CODE HERE f = filtering(D, 'playerID', T) H = sum_column(f, 'H') _2B = sum_column(f, '2B') _3B = sum_column(f, '3B') HR = sum_column(f, 'HR') BB = sum_column(f, 'BB') AB = sum_column(f, 'AB') RC = runs_created(H, _2B, _3B, HR, BB, AB) ######################################### return RC ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test6.py:test_runs' in the terminal. '''
def sum_salaries(T, D): ''' Given a team of players (T), compute the sum of salaries of all the players in the team. Input: T: a python list of playerID's, for example, ['zitoba01', 'hiljuer01', ...] D: a data frame loaded from 'Batting2001AJS.csv', which we processed in problem4.py. Output: S: an integer scalar, the sum of salaries of all the players in the team. ''' ######################################### ## INSERT YOUR CODE HERE Salary = filtering(D, 'playerID', T) S = sum_column(Salary, 'salary') ######################################### return S ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test6.py:test_sum_salaries' in the terminal. '''
def sum_stat(T, D, key='H'): ''' Given a team of players (T), compute the sum of game statistics of all the players in the team. For example, suppose we have a team with two players, the number of hits: 100, 200. Then the sum of hits in the team will be: 100+200 = 300 Input: T: a python list of playerID's, for example, ['zitoba01', 'hiljuer01', ...] D: a data frame loaded from 'Batting2001AJS.csv', which we processed in problem4.py. key: the column to be summed, for example, 'H' represents the number of hits Output: S: an integer scalar, the sum of statistics of all the players in the team. ''' ######################################### ## INSERT YOUR CODE HERE df = filtering(D, 'playerID', T) S = sum_column(df, key) ######################################### return S ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test6.py:test_sum_stat' in the terminal. '''