Get startedGet started for free

Analyzing baseball stats with .apply()

The Tampa Bay Rays want you to analyze their data.

They'd like the following metrics:

  • The sum of each column in the data
  • The total amount of runs scored in a year ('RS' + 'RA' for each year)
  • The 'Playoffs' column in text format rather than using 1's and 0's

The below function can be used to convert the 'Playoffs' column to text:

def text_playoffs(num_playoffs): 
    if num_playoffs == 1:
        return 'Yes'
    else:
        return 'No' 

Use .apply() to get these metrics. A DataFrame (rays_df) has been loaded and printed to the console. This DataFrame is indexed on the 'Year' column.

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.

# Gather sum of all columns
stat_totals = ____.____(____, axis=____)
print(stat_totals)
Edit and Run Code