Mission 345 Slide 2 - doesn't understand the instruction "pass it into the function without the parentheses"

on Mission 345 Slide 2 - Apply a Function Element-wise Using the Map and Apply Methods. In the lesson there is this statement

Note that these methods both take a function as a parameter. Because we’re using the function as a parameter, we pass it into the function without the parentheses. For example, if we were working with a function called transform , we’d pass it into the apply() method as follows:

def transform(val):

return val


The problem is i don’t see where the parentheses is missing? Sorry, where am i missing out on this? Is the author trying to meant parameters - https://stackoverflow.com/questions/49104825/why-dont-i-pass-any-parameters-to-a-function-within-map

Hi, willx.

The missing parentheses are after transform. Normally when you call a function you need to put the function inputs inside parentheses just after the function name.

For example, if we make a function “sqrt”, that calculates the square root of a number:

def sqrt(number):
    return number**(0.5)

Then, to use the function, normally you code:


The output would be 2.

What is special about using a function as a parameter for series.apply(), is that you don’t have to specify the function inputs in parentheses, because series.apply() is coded to use the values of the series as the inputs to the function you are applying.

Please let me know if I am not clear with the explanation and I can try another example.


hi mmanderson1

let me try to understand this.

Series.apply(transform) has the transform function being called but not having the parameters in parentheses passed after is it?

if a normal function, it would have been like apply(transform(val)) ?

sorry, maybe another example to clear this may help if possible :grinning:

Yes, I think you’ve got it. Here is a more complete example:

import pandas as pd 
series1 = pd.Series([4, 9, 16, 25])
def sqrt(number):
    return number**(0.5)
series2 = series1.apply(sqrt)

If you run the above code, you can see that the apply method steps through each item in the Pandas series and uses it as number in the sqrt function.

Got it. thank you:grinning: so much