Updating user-defined function to do subtraction
The function you worked on in the previous exercise cannot do subtraction. Have a look at this function:
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
This function can deal with addition and subtraction in numerators and denominators of financial ratios. Notice that the function uses np.where
. This is a function from the package NumPy. np.where
checks if the first argument is True
; if so, it returns the second argument, else it returns the third. For example, in the above, we have:
np.where(addition_in_numerator,
df[numerator].sum(axis=1),
df[numerator[0]] - df[numerator[1:]].sum(
axis=1))
If addition_in_numerator
is true, np.where
will return df[numerator].sum(axis=1)
, else it will return df[numerator[0]] - df[numerator[1:]].sum(axis=1)
.
In this exercise, the balance_sheet
DataFrame, along with pandas and NumPy aspd
and np
, respectively, have been loaded for you. Use these to determine which of the following statements is correct.
Este exercício faz parte do curso
Analyzing Financial Statements in Python
Exercício interativo prático
Transforme a teoria em ação com um de nossos exercícios interativos
