Get startedGet started for free

Mapping functions

R has the *apply family of functions that can take a function and apply it to several or all elements of a list/data.frame/matrix.

The Python equivalent is the built-in map() function. map() takes the name of the function as its first argument, and then a list of values as the second argument. The specified function is then applied to all the values in the second argument one-at-a-time. Recall that you need to wrap the output of map() inside list() to get the desired results.

In the third exercise of this chapter, you used a for loop to determine the binge status for all the elements in num_drinks. In this exercise, you will use map() along with the functions you defined in exercise five to accomplish the same.

This exercise is part of the course

Python for R Users

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# map the binge_male function to num_drinks
print(list(map(____, ____)))
Edit and Run Code