MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Applying Custom Functions (apply, map, applymap)0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
5.1Applying Custom Functions (apply, map, applymap)5.2Vectorized String Methods (.str accessor)5.3Renaming Columns, Replacing Values & Sorting
HomeTutorialsPandasApplying Custom Functions (apply, map, applymap)
Vectorized String Methods (.str accessor)

Applying Custom Functions (apply, map, applymap)

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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() (formerly applymap in older Pandas): Apply a function element-wise across the entire table.

2. Using Series.map() for Value Substitution

1import pandas as pd
2 
3df = pd.DataFrame({
4 'Employee': ['Aarav', 'Priya', 'Rohan'],
5 'Gender_Code': ['M', 'F', 'M'],
6 'Grade': ['A', 'B', 'A']
7})
8 
9# Dictionary mapping for quick translation
10gender_map = {'M': 'Male', 'F': 'Female'}
11df['Gender'] = df['Gender_Code'].map(gender_map)
12print(df)

3. Using Series.apply() with Custom Functions & Lambdas

1def calculate_tax(salary):
2 if salary > 100000:
3 return salary * 0.30
4 elif salary > 50000:
5 return salary * 0.20
6 else:
7 return salary * 0.05
8 
9df_salaries = pd.DataFrame({'Emp': ['A', 'B', 'C'], 'Salary': [120000, 65000, 40000]})
10 
11# Apply custom function to Salary column
12df_salaries['Tax_INR'] = df_salaries['Salary'].apply(calculate_tax)
13 
14# Or with an inline lambda function
15df_salaries['Take_Home'] = df_salaries.apply(lambda row: row['Salary'] - row['Tax_INR'], axis=1)
16print(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• Knowledge Check

Practice Quiz

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 LessonNext Lesson
Handling Duplicates & Data Type ConversionsVectorized String Methods (.str accessor)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext