Skip to main content
Editor

Pandas apply(), map(), transform(): Custom Data Transformations

Advanced25 min6 exercises110 XP
0/6 exercises

Pandas gives you dozens of built-in methods like .sum(), .mean(), and .str.upper(). But what happens when you need to run your own custom logic on every row, column, or cell? That is where apply(), map(), and transform() come in.

Think of these methods as assembly lines. You feed in your data, each item passes through your custom function, and transformed results come out the other end. The trick is knowing which assembly line to use for each job.

In this tutorial, you'll learn the differences between apply(), map(), and transform(), when to use each one, and why vectorized operations should be your first choice whenever possible.

What Does map() Do on a Series?

Series.map() applies a function (or a dictionary) to every single value in a Series. It is the simplest transformation tool -- one value goes in, one value comes out.

Series.map() with functions and dictionaries
import pandas as pd
names = pd.Series(['alice', 'bob', 'carol'])
# Map with a function
print(names.map(str.title))
print()
# Map with a dictionary (for lookups/replacements)
grade_map = {'A': 'Excellent', 'B': 'Good', 'C': 'Average'}
grades = pd.Series(['A', 'B', 'C', 'A'])
print(grades.map(grade_map))
Output
0    Alice
1      Bob
2    Carol
dtype: object

0    Excellent
1         Good
2      Average
3    Excellent
dtype: object

How Does apply() Work on a Series?

Series.apply() is very similar to map() but is slightly more flexible. It supports passing extra arguments to your function and works better with more complex logic. For simple cases, map() and apply() on a Series are interchangeable.

Series.apply() with lambdas and extra args
import pandas as pd
prices = pd.Series([10.5, 20.0, 35.75, 8.25])
# apply with a lambda
discounted = prices.apply(lambda p: round(p * 0.9, 2))
print('10% discount:', discounted.tolist())
# apply with extra arguments
def apply_tax(price, rate):
return round(price * (1 + rate), 2)
taxed = prices.apply(apply_tax, rate=0.08)
print('With 8% tax:', taxed.tolist())
Output
10% discount: [9.45, 18.0, 32.18, 7.42]
With 8% tax: [11.34, 21.6, 38.61, 8.91]

How Does apply() Work on a DataFrame?

When you call apply() on a DataFrame, the function receives an entire column (by default, axis=0) or an entire row (axis=1). This is different from Series apply, which receives one scalar value at a time.

DataFrame.apply() with axis=0 vs axis=1
import pandas as pd
df = pd.DataFrame({
'math': [90, 80, 70],
'science': [85, 95, 75],
'english': [88, 78, 92]
})
# axis=0 (default): function receives each COLUMN
print('Column means:')
print(df.apply(lambda col: col.mean()).to_string())
print()
# axis=1: function receives each ROW
print('Row averages:')
print(df.apply(lambda row: round(row.mean(), 1), axis=1).tolist())
Output
Column means:
math       80.0
science    85.0
english    86.0

Row averages:
[87.7, 84.3, 79.0]

A useful pattern with axis=1 is creating new columns that depend on multiple existing columns. For example, computing a weighted score from several subject grades.

Creating columns from row-level logic
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Carol'],
'math': [90, 80, 70],
'science': [85, 95, 75]
})
# Create a pass/fail column based on both scores
def status(row):
if row['math'] >= 80 and row['science'] >= 80:
return 'Honors'
elif row['math'] >= 70 and row['science'] >= 70:
return 'Pass'
return 'Fail'
df['status'] = df.apply(status, axis=1)
print(df.to_string(index=False))
Output
 name  math  science status
Alice    90       85 Honors
  Bob    80       95 Honors
Carol    70       75   Pass

What About DataFrame.map()?

In older versions of Pandas, applymap() applied a function to every single cell in a DataFrame. Since Pandas 2.1, this has been renamed to DataFrame.map(). It works element-wise -- your function receives one scalar value at a time, not a row or column.

DataFrame.map() for element-wise operations
import pandas as pd
df = pd.DataFrame({
'A': [1.2345, 2.6789],
'B': [3.1415, 4.9999]
})
# Round every cell to 1 decimal place
rounded = df.map(lambda x: round(x, 1))
print(rounded)
Output
     A    B
0  1.2  3.1
1  2.7  5.0

When Should You Use transform()?

transform() is a stricter version of apply(). It guarantees that the output has the exact same shape as the input. If your function tries to return fewer rows or a different shape, Pandas will raise an error. This makes it perfect for normalizing data or adding group-level statistics back to each row.

transform() for within-group normalization
import pandas as pd
df = pd.DataFrame({
'team': ['A', 'A', 'B', 'B'],
'score': [10, 20, 30, 40]
})
# transform: normalize scores within each team (z-score style)
grouped = df.groupby('team')['score']
means = grouped.transform('mean')
stds = grouped.transform('std')
df['z_score'] = ((df['score'] - means) / stds).round(2)
print(df.to_string(index=False))
Output
team  score  z_score
   A     10    -0.71
   A     20     0.71
   B     30    -0.71
   B     40     0.71

Why Should You Avoid apply() When Possible?

Every call to apply() runs a Python function in a loop. That is orders of magnitude slower than vectorized Pandas or NumPy operations, which run in optimized C code. For simple math, string operations, or conditional logic, there is almost always a vectorized alternative.

Slow: apply()
# Runs Python loop per row
df['discounted'] = df['price'].apply(
    lambda p: p * 0.9
)
Fast: vectorized
# Runs in C under the hood
df['discounted'] = df['price'] * 0.9

Practice Exercises

Map Values with a Dictionary
Write Code

Use Series.map() with a dictionary to convert letter grades to GPA points. Map: A=4.0, B=3.0, C=2.0, D=1.0, F=0.0.

Print the resulting GPA Series as a list.

import pandas as pd
grades = pd.Series(['A', 'B', 'C', 'A', 'F', 'B'])
# Map grades to GPA points and print as a list
Row-Level Apply
Write Code

Use apply() with axis=1 to create a new column label in the DataFrame. The label should be the name followed by the age in parentheses, like "Alice (30)".

Print the label column as a list.

import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Carol'],
'age': [30, 25, 35]
})
# Create a label column using apply with axis=1
Predict the map() Output
Predict Output

What will this code print?

import pandas as pd
s = pd.Series([1, 2, 3, 4])
print(s.map(lambda x: x ** 2 if x % 2 == 0 else x).tolist())
# Type the exact output
Element-wise DataFrame Formatting
Write Code

Use DataFrame.map() to format every numeric cell in df as a string with a dollar sign and 2 decimal places (e.g., "$10.50").

Print the first row as a list: print(df.iloc[0].tolist()).

import pandas as pd
df = pd.DataFrame({
'Q1': [10.5, 20.0],
'Q2': [15.75, 30.25]
})
# Format every cell as a dollar string
Refactor apply() to Vectorized
Refactor

This code uses apply() to compute a 10% discount. Refactor it to use vectorized operations instead (no apply, no map, no loops). The output should be identical.

Print the discounted column as a list.

import pandas as pd
df = pd.DataFrame({'price': [100, 200, 50, 75]})
# Refactor this apply() to use vectorized math
df['discounted'] = df['price'].apply(lambda p: round(p * 0.9, 2))
print(df['discounted'].tolist())
Fix the apply() Bug
Fix the Bug

This code tries to create a total column that sums math and science scores for each student using apply(). But it gives wrong results because of a missing argument. Fix the bug.

Print the total column as a list.

import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob'],
'math': [90, 80],
'science': [85, 95]
})
# Bug: this produces wrong results
df['total'] = df[['math', 'science']].apply(lambda row: row['math'] + row['science'])
print(df['total'].tolist())

Harder problems

More problems on this topic: Working with data

Related Tutorials

Was this tutorial helpful?