Aan de slagGa gratis aan de slag

Functions with additional arguments

Let's add some arguments to the function definition!

Numeric data in scores represent students' performance scaled between 0 and 100. Your task is to rescale this data to an arbitrary range between low and high. Rescaling should be done in a linear fashion, i.e. for any data point \(x\) in a column:

\(x_{new}\) = \(x\frac{high -low}{100} + low\)

To do rescaling, you'll have to define the function rescale(). Remember, the operation written above can be applied to Series directly. After defining the function, you'll have to apply it to the specified columns of scores.

Deze oefening maakt deel uit van de cursus

Practicing Coding Interview Questions in Python

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

def rescale(series, low, high):
   # Define the expression to rescale input series
   return ____

# Rescale the data in cols to lie between 1 and 10
cols = ['math score', 'reading score', 'writing score'] 
scores[cols] = scores[cols].____
print(scores[cols].head())
Code bewerken en uitvoeren