Get startedGet started for free

Computing ratios with user-defined function

In this exercise, you'll use the function from the last exercise to compute financial ratios. Recall that the function is:

def compute_ratio(df, numerator, denominator, ratio_name, 
                  addition_in_numerator = True,
                  addition_in_denominator = True):
  numerator_of_ratio = np.where(addition_in_numerator,
                             df[numerator].sum(axis=1), 
                             df[numerator[0]] - df[numerator[1:]].sum(
                               axis=1))
  denominator_of_ratio = np.where(addition_in_denominator, 
                               df[denominator].sum(axis=1), 
                               df[denominator[0]] - df[denominator[1:]].sum(axis=1))
  df[ratio_name] = numerator_of_ratio/denominator_of_ratio
  return df

The pandas DataFrame merged_dat is loaded for you, which you will use to compute financial ratios using the function compute_ratio.

This exercise is part of the course

Analyzing Financial Statements in Python

View Course

Hands-on interactive exercise

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

# Print the columns of merged_dat
print(merged_dat____)
Edit and Run Code