Data Science

Applying Custom Functions with .apply()

SQL Mastery Team
May 17, 2026
5 min read

It's **Day 116**, and we're breaking free from built-in functions. Sometimes شما have specialized business logic that Pandas doesn't have a button for.

The .apply() method

`.apply()` takes a function and runs it across an entire column or even the entire row.

def check_email_provider(email):

if '@gmail.com' in email:

return 'Google'

return 'Other'

df['provider'] = df['email'].apply(check_email_provider)

Lambda Functions (The Pro Way)

For simple logic, we use "Lambdas" (anonymous one-line functions).

# Multiply price by tax based on a complicated condition

df['taxed_price'] = df['price'].apply(lambda x: x * 1.2 if x > 100 else x * 1.05)

Warning: Performance

`.apply()` is essentially a Loop in disguise. It is much slower than "Vectorized" Pandas functions (like `df['price'] * 1.1`). Only use `.apply()` when there is no other way!

Your Task for Today

Write a function that categorizes ages into 'Child', 'Adult', and 'Senior' and apply it to a column.

*Day 117: String Manipulation in Pandas.*

Ready to put your knowledge into practice?

Join SQL Mastery and learn through interactive exercises.