def hits_per_pitch_subzone(df): """ Get hits per pitch in a sub-region of the strike zone Input: dataframe containing pitches within a specific sub-region output: dataframe with rows = vertical position, columns = horizontal position, values = Hits per pitch in that subzone """ hits_subzoneD = {} for row in np.arange(-1.5, 1.5, 0.6): for col in np.arange(1, 4, 0.6): subzone = df[(df['px']>=row) & (df['px']<row+0.6) & (df['pz']>=col) & (df['pz']<col+0.6)] hitsD = Baseball.hits_tb_per_pitch(subzone) hits = hitsD['Hits'] pitches = hitsD['Pitches'] try: hits_per_pitch = hits/pitches except ZeroDivisionError: hits_per_pitch = 0 try: hits_subzoneD[row].append( hits_per_pitch ) except KeyError: hits_subzoneD[row] = [hits_per_pitch, ] return pd.DataFrame(hits_subzoneD, index=np.arange(1, 4, 0.6))
def tb_per_pitch_subzone(df): """ Get total bases in a sub-region of the strike zone Input: dataframe containing pitches within a specific sub-region output: dataframe with rows = vertical position, columns = horizontal position, values = TB per pitch in that subzone """ tb_subzoneD = {} for row in np.arange(-1.5, 1.5, 0.6): for col in np.arange(1, 4, 0.6): subzone = df[(df['px']>=row) & (df['px']<row+0.6) & (df['pz']>=col) & (df['pz']<col+0.6)] tbD = Baseball.hits_tb_per_pitch(subzone) try: tb_subzoneD[row].append( tbD['TB_per_pitch']) except KeyError: tb_subzoneD[row] = [tbD['TB_per_pitch'], ] return pd.DataFrame(tb_subzoneD, index=np.arange(1, 4, 0.6))