Applying Functions with apply and map
What you'll learn: How to transform your data efficiently by applying custom functions across rows, columns, or individual cells.
Why This Matters for Feature Engineering
When preparing data for machine learning, you often need to create new features by transforming existing ones. Rather than writing loops (slow!), Pandas gives you three powerful tools: apply, map, and applymap (older versions) or map on DataFrames (newer versions).
The Core Idea
Think of these methods as assembly line workers:
map(on Series): Transforms each value in a single column, one at a timeapply(on Series or DataFrame): Applies a function along an axis—either to each column or each rowapplymap(on DataFrame): Applies a function to every individual cell
When to Use Each
Use map when you want to transform values in one column based on a dictionary or simple function:
- Converting categorical codes to labels
- Creating binary flags from thresholds
Use apply when you need:
- To create features that combine multiple columns (apply across rows)
- To aggregate or transform entire columns (apply down columns)
- Custom logic that can't be vectorized
Use applymap when transforming every cell uniformly (e.g., rounding all numbers, converting all values to strings).
Practical ML Example
Imagine you have temperature data in Celsius and need it in Fahrenheit—that's map. If you need to calculate BMI from height and weight columns—that's apply across rows. If you want to round all numeric values in a dataset—that's applymap.
Key Takeaway: apply and map let you create custom features by applying functions flexibly across your data—essential for feature engineering before model training.