The apply() Function
The apply() function lets you apply custom functions to rows, columns, or individual elements.
Apply to Columns
Apply a function to each column:
Loading Pandas Playground...
Apply to Rows
Use axis=1 to apply across rows:
Loading Pandas Playground...
Lambda Functions
Use lambda for simple operations:
Loading Pandas Playground...
Apply with Multiple Arguments
Pass additional arguments to your function:
Loading Pandas Playground...
applymap() for Element-wise
Apply function to every element:
Loading Pandas Playground...
Apply vs Vectorized Operations
Vectorized operations are usually faster:
Loading Pandas Playground...
Exercise: Custom Calculation
Loading Exercise...
Exercise: Transform Values
Loading Exercise...
Key Points
apply(func)applies function to columns (axis=0)apply(func, axis=1)applies to rows- Lambda functions for simple transformations
- Pass extra args via lambda or using
args= - Vectorized operations are faster when possible
- Use apply when you need complex logic

