I have a dataframe in python that has many columns of baseball data. Every pitch is another row and specifically for this problem two of the columns are “Inning”: which is the inning the pitch took place in, and “Outs”: which is how many outs at the time of the pitch. I am trying to create a function that calculates Innings pitched for a pitcher which is calculated as follows. If a pitcher pitches 3 innings and gets taken out in the 4th while they got the first 2 outs, they will have pitched 3.2 innings. if they had pitched 1 out then it would be 3.1 outs. The data frame has multiple different games so I need to add up all the innings pitched in the different games (for example if game 1 they pitched 3.1 innings and game 2 was 4.2 innings this would be 8 innings pitched). This is the function I have right now:
def calc_innings_pitched(pitcher_df):
innings_pitched = 0.0
for date, game_df in pitcher_df.groupby('Date'):
max_inning = game_df['Inning'].max()
last_row = game_df.iloc[-1]
outs = last_row['Outs']
innings = max_inning + (outs / 3.0)
innings_pitched += innings
if innings_pitched % 1 == (1/3):
return int(innings_pitched) + 0.1
elif innings_pitched % 1 == (1/3):
return int(innings_pitched) + 0.2
else:
return innings_pitched
Ive tried a couple of methods on my own and a couple of chat gpt generated answers but none can calculate it correctly.
Brennan G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.