Status
Not started
7. Function Application
Apply functions row-wise, column-wise, element-wise, or via a clean pipe.
df.apply(func, axis=0)
→ Apply function along an axis (0
= columns,1
= rows).df.applymap(func)
→ Apply function element-wise.df.agg()
ordf.aggregate()
→ Aggregate using one or more operations.df.transform()
→ Transform rows/columns; shape is preserved.df.pipe(func)
→ Pipe DataFrame through a custom function.
Pandas Function Application - Comprehensive Guide
Table of Contents
- df.apply() - Row-wise and Column-wise Operations
- df.applymap() - Element-wise Operations
- df.agg() / df.aggregate() - Aggregation Operations
- df.transform() - Shape-preserving Transformations
- df.pipe() - Function Chaining
- Practical Examples with Multiple Datasets
- Performance Tips
Setup and Data Loading
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Load sample datasets
tips = sns.load_dataset('tips')
flights = sns.load_dataset('flights')
titanic = sns.load_dataset('titanic')
iris = sns.load_dataset('iris')
print("Datasets loaded:")
print(f"Tips shape: {tips.shape}")
print(f"Flights shape: {flights.shape}")
print(f"Titanic shape: {titanic.shape}")
print(f"Iris shape: {iris.shape}")
‣
1. df.apply()
- Row-wise and Column-wise Operations
‣
2. df.applymap() - Element-wise Operations
‣
3. df.agg() / df.aggregate() - Aggregation Operations
‣
4. df.transform() - Shape-preserving Transformations
‣
5. df.pipe() - Function Chaining
‣
6. Practical Examples with Multiple Datasets
‣
7. Performance Tips
Summary
This guide covered the five main function application methods in pandas:
df.apply()
: Apply functions along rows or columnsdf.applymap()
: Apply functions element-wise to entire DataFramedf.agg()
/df.aggregate()
: Aggregate data using one or more operationsdf.transform()
: Transform data while preserving DataFrame shapedf.pipe()
: Chain custom functions for clean, readable data pipelines
When to Use Each Method:
apply()
: When you need row-wise or column-wise operations that can't be vectorizedapplymap()
: When you need to transform every single element in the DataFrameagg()
: When you need to compute summary statistics or aggregationstransform()
: When you need to modify data but keep the same shape (e.g., standardization)pipe()
: When you want to chain multiple operations for clean, readable code
Performance Hierarchy (fastest to slowest):
- Vectorized operations (direct arithmetic)
- Built-in pandas methods
transform()
andagg()
apply()
with NumPy functionsapply()
with custom Python functionsapplymap()
for element-wise operations
Remember: Always prefer vectorized operations when possible, and use these methods when vectorization isn't feasible or when you need specific functionality they provide.