Get startedGet started for free

Run differentials with .iterrows()

You've been hired by the San Francisco Giants as an analyst—congrats! The team's owner wants you to calculate a metric called the run differential for each season from the year 2008 to 2012. This metric is calculated by subtracting the total number of runs a team allowed in a season from the team's total number of runs scored in a season. 'RS' means runs scored and 'RA' means runs allowed.

The below function calculates this metric:

def calc_run_diff(runs_scored, runs_allowed):

    run_diff = runs_scored - runs_allowed

    return run_diff

A DataFrame has been loaded into your session as giants_df and printed into the console. Let's practice using .iterrows() to add a run differential column to this 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.

# Create an empty list to store run differentials
____ = ____
Edit and Run Code