LoslegenKostenlos loslegen

Additional arguments

Let's use additional arguments in the .apply() method!

Your task is to create two new columns in scores:

  • mean is the row-wise mean value of the math score, reading score and writing score
  • rank defines how high the mean score is:
    • 'high' if the mean value \(> 90\)
    • 'medium' if the mean value \(> 60\) but \(\leq\) 90
    • 'low' if the mean value \(\leq 60\)

To accomplish this task, you'll need to define the function rank that, given a series, returns a list with two values: the mean of the series and a string defined by the aforementioned rule.

The module numpy is already imported for you as np.

Diese Übung ist Teil des Kurses

Practicing Coding Interview Questions in Python

Kurs anzeigen

Anleitung zur Übung

  • Calculate the mean of the input series.
  • Return the mean and its rank as a list.
  • Insert the output of rank() into new columns of scores.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

def rank(series):
    # Calculate the mean of the input series
    mean = ____
    # Return the mean and its rank as a list
    if ____:
        return ____
    if ____:
        return ____
    return ____

# Insert the output of rank() into new columns of scores
cols = ['math score', 'reading score', 'writing score']
scores[['mean', 'rank']] = scores[cols].____
print(scores[['mean', 'rank']].head())
Code bearbeiten und ausführen