Applying Custom Functions (apply, map, applymap)0%
Applying Custom Functions (apply, map, applymap)
Beginner12 min read•Updated: Jul 11, 2026
Applying Custom Functions (apply, map, applymap)
Reading Time: 8 Mins | Level: Beginner to Advanced
1. When Vectorization Isn't Enough
Pandas built-in vectorized operations are always the fastest approach. However, real-world business logic often requires custom branching (e.g. tax calculations, complex regex transformations, or calling external APIs). For these tasks, Pandas provides:
Series.map(): Map values of a Series using a dictionary or 1-to-1 function.Series.apply()/DataFrame.apply(): Apply arbitrary functions across a Series or along DataFrame axes.DataFrame.map()(formerlyapplymapin older Pandas): Apply a function element-wise across the entire table.
2. Using Series.map() for Value Substitution
Python
import pandas as pd
df = pd.DataFrame({
'Employee': ['Aarav', 'Priya', 'Rohan'],
'Gender_Code': ['M', 'F', 'M'],
'Grade': ['A', 'B', 'A']
})
# Dictionary mapping for quick translation
gender_map = {'M': 'Male', 'F': 'Female'}
df['Gender'] = df['Gender_Code'].map(gender_map)
print(df)
3. Using Series.apply() with Custom Functions & Lambdas
Python
def calculate_tax(salary):
if salary > 100000:
return salary * 0.30
elif salary > 50000:
return salary * 0.20
else:
return salary * 0.05
df_salaries = pd.DataFrame({'Emp': ['A', 'B', 'C'], 'Salary': [120000, 65000, 40000]})
# Apply custom function to Salary column
df_salaries['Tax_INR'] = df_salaries['Salary'].apply(calculate_tax)
# Or with an inline lambda function
df_salaries['Take_Home'] = df_salaries.apply(lambda row: row['Salary'] - row['Tax_INR'], axis=1)
print(df_salaries)
Pro Tip
When applying a function across multiple columns of a single row, pass
axis=1 so the lambda receives the row as a Series!4. Performance Warning: The Cost of apply()
Unlike native vectorized methods (which run compiled in C), .apply() is effectively a Python-level loop under the hood.
Always prefer native vectorized math where possible:
- Fast:
df['Total'] = df['Price'] * df['Qty'](Vectorized C speed) - Slow:
df['Total'] = df.apply(lambda r: r['Price'] * r['Qty'], axis=1)(Python loop speed)
Interactive Quiz
Multiple Choice Questions
Question 1
Which method is best suited for replacing values in a Series using a dictionary lookup table?
Question 2
When calling
df.apply(custom_func, axis=1), what does the function receive as its argument?Question 3
In Pandas 2.1+, which method replaces the deprecated
DataFrame.applymap() for applying a function element-wise to every cell in a DataFrame?Question 4
Why should you avoid using
df.apply(..., axis=1) for simple arithmetic operations like Col_A + Col_B?Question 5
What will
pd.Series(['delhi', 'mumbai']).apply(str.upper) return?Next Lesson
Vectorized String Methods (.str accessor)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Handling Duplicates & Data Type Conversions | Vectorized String Methods (.str accessor) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.