Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Master Data Science with Python

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

Info

Avoid apply on large DataFrames; vectorized alternatives are orders of magnitude faster.

Previous: Categorical Data and Encoding TechniquesNext: Pandas Project: Titanic Data Analysis