Get startedGet started for free

Bringing it all together: Predict win percentage

A pandas DataFrame (baseball_df) has been loaded into your session. For convenience, a dictionary describing each column within baseball_df has been printed into your console. You can reference these descriptions throughout the exercise.

You'd like to attempt to predict a team's win percentage for a given season by using the team's total runs scored in a season ('RS') and total runs allowed in a season ('RA') with the following function:

def predict_win_perc(RS, RA):
    prediction = RS ** 2 / (RS ** 2 + RA ** 2)
    return np.round(prediction, 2)

Let's compare the approaches you've learned to calculate a predicted win percentage for each season (or row) in your DataFrame.

This exercise is part of the course

Writing Efficient Python Code

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

win_perc_preds_loop = []

# Use a loop and .itertuples() to collect each row's predicted win percentage
for ____ in baseball_df.____():
    runs_scored = ____.____
    runs_allowed = ____.____
    win_perc_pred = predict_win_perc(____, ____)
    win_perc_preds_loop.append(____)
Edit and Run Code