© Hannah Stepanek 2020
H. StepanekThinking in Pandashttps://doi.org/10.1007/978-1-4842-5839-2_6

6. The apply Method

Hannah Stepanek1 
(1)
Portland, OR, USA
 

apply is one of the most incorrectly used functions in pandas. Chances are if you are using it, you shouldn’t be. This is because apply “applies” the function to each row or each column in the data set effectively breaking one of the cardinal rules of using pandas: do not iterate over the data set. In this chapter, we’ll explore when apply is the right choice and present alternative solutions for when it’s not.

When not to use apply

For those comfortable with basic programing features, iteration is a familiar way to manipulate data. We think to ourselves: I would like to run this operation on every ...

Get Thinking in Pandas: How to Use the Python Data Analysis Library the Right Way now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.