5

Apply Functions

Learning about .apply() is fundamental in the data cleaning process. It also encapsulates key concepts in programming, mainly writing functions. The .apply() method takes a function and applies it (i.e., runs it) across each row or column of a DataFrame without having you write the code for each element separately.

If you’ve programmed before, then the concept of an apply should be familiar. It is similar to writing a for loop across each row or column and calling the function, or making a map() call to a function. In general, this is the preferred way to apply functions across dataframes, because it typically is much faster than writing a for loop in Python.

If you haven’t programmed before, then prepare to see how we can ...

Get Pandas for Everyone: Python Data Analysis, 2nd Edition 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.