Lesson 29 of 60 · python
Applying Functions: `apply`, `map`, and Vectorized Operations
Duration: 20 minutes
Applying Functions in Pandas
Pandas allows you to apply custom Python logic across rows or columns.
Using map for element‑wise transformation
df['temp_f'] = df['temp_c'].map(lambda c: c * 9/5 + 32)
Using apply on a Series
df['name'] = df['name'].apply(str.title)
Using apply on a DataFrame (row‑wise)
def compute_score(row):
return row['units'] * row['price'] * 0.9 # 10% discount
df['score'] = df.apply(compute_score, axis=1)
Vectorized operations (preferred when possible)
df['revenue'] = df['units'] * df['price']
Using transform for broadcasting results back to original shape
# Z‑score per group
zscore = df.groupby('store_id')['revenue'].transform(lambda x: (x - x.mean()) / x.std())
df['revenue_z'] = zscore