22.5: Passing Aggregate Data to Functions
- Page ID
- 88721
Even though the previous example involved passing atomic data to a function, you can totally pass aggregate data as well. Suppose we’d like to be able to easily compute the IQR (recall p. 150) of a univariate data set. Writing a function to do that is a snap:
Code \(\PageIndex{1}\) (Python):
def IQR(some_data):
return some_data.quantile(.75) - some_data.quantile(.25)
We can now call it on anything we like, like our examples from p. 150 and p. 159:
Code \(\PageIndex{2}\) (Python):
print("The IQR of the YouTube plays data is {}".format(IQR(num_plays)))
print("The IQR of the NCAA scoring data is {}".format(IQR(pts)))
| The IQR of the YouTube plays data is 412.
| The IQR of the NCAA scoring data is 15.
Again, we named the function’s own argument (some_data) something different than the variables it was called with (num_plays first, and then pts). This is a happy and healthy thing.