Applying functions to transform data

In situations where a direct mapping or substitution will not suffice, it is possible to apply a function to the data to perform an algorithm on the data. Pandas provides the ability to apply functions to individual items, entire columns, or entire rows, providing incredible flexibility in transformation.

Functions can be applied using the conveniently named .apply() method. When given a Python function, this method iteratively calls the function while passing in each value from a Series. If applied to a DataFrame, pandas will pass in each column as a Series, or if applied along axis=1, it will pass in a Series representing each row.

The following demonstrates this by applying a lambda function to each ...

Get Learning pandas - Second 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.