Pandas apply, map and transform
last modified November 17, 2026 Python 3.15
Pandas offers several ways to run a function over data. The three most
useful are map, apply and transform.
They differ in what they are called on and in the shape of the object they
return, so choosing the right one saves a lot of reshaping later.
A Series is one column of data, a DataFrame is a table
of columns, and the shape of an object tells us how many rows and
columns it has. As a rule of thumb: map and element-wise
apply keep a Series the same length, transform
keeps the original shape, and column-wise apply reduces each
column to one value.
Series.map with a function
Called on a Series, map applies a function to every value and
returns a new Series of the same length. The index is unchanged; only the
values are replaced. This is the simplest way to transform a column.
import pandas as pd s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) print(s.map(lambda x: x * 2))
The lambda x: x * 2 is a small anonymous function that doubles
one value. map calls it four times, once per element, and
returns four values with the same labels a, b,
c, d. Any ordinary function can be used instead of
a lambda.
$ python map_function.py a 2 b 4 c 6 d 8 dtype: int64
Series.map with a dictionary
Instead of a function, map accepts a dictionary. The dictionary
acts as a lookup table: each value in the Series is replaced by the value
stored under that key. Keys that are missing from the dictionary become
NaN, the standard Pandas marker for a missing value.
import pandas as pd
s = pd.Series(['a', 'b', 'c', 'a'])
print(s.map({'a': 10, 'b': 20}))
Here a becomes 10 and b becomes 20. There is no key
for c, so that position shows NaN. Because a
missing value appears, the result's data type changes from integers to
floating-point numbers, which is why the numbers are printed as
10.0 and 20.0.
$ python map_dict.py 0 10.0 1 20.0 2 NaN 3 10.0 dtype: float64
Series.apply
Series.apply also runs a function on every element. When the
function returns a single value, the result is a Series with the same length
as the input, just like map. The difference is that
apply can also handle functions that return several values per
element, which map is not meant for.
import pandas as pd s = pd.Series([1, 2, 3, 4]) print(s.apply(lambda x: x ** 2))
The function x ** 2 squares each value. The result is a Series
with the same four integer positions 0 to 3. For
simple one-to-one changes, map and apply are
interchangeable; reach for apply when the function is more
involved.
$ python series_apply.py 0 1 1 4 2 9 3 16 dtype: int64
DataFrame.apply with axis=0
On a DataFrame, apply works on whole columns or whole rows.
With the default axis=0, the function receives one column at a
time and must return one value for it. The result is a Series indexed by the
column names: one value per column.
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3], 'b': [10, 20, 30]})
print(df.apply(lambda col: col.max()))
The function col.max() returns the largest value of a column.
Because there are two columns, the result has two values, labelled
a and b. This is how you compute a summary for each
column without typing each one separately.
$ python df_apply_columns.py a 3 b 30 dtype: int64
DataFrame.apply with axis=1
With axis=1, the function receives one row at a time. The result
is again a Series, but now it has one value per row and is indexed by
the row labels. This is useful when a new value depends on several columns of
the same row.
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3], 'b': [10, 20, 30]})
print(df.apply(lambda row: row['a'] + row['b'], axis=1))
Each row is passed in as a Series, so row['a'] + row['b'] adds
the two cells of that row. The output has three entries, labelled
0, 1 and 2, matching the three rows of
the original DataFrame.
$ python df_apply_rows.py 0 11 1 22 2 33 dtype: int64
DataFrame.map (pandas 2.1+)
DataFrame.map applies a function to every single cell and
returns a DataFrame with exactly the same shape and labels as the original.
It was introduced in pandas 2.1 as the new name for applymap.
Use it when a change must happen element by element across many columns.
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
print(df.map(lambda x: x * 10))
Every value in the two-column, three-row table is multiplied by ten. The
result keeps the two columns a and b and the three
row labels; only the numbers change. Comparing this with
DataFrame.apply makes the difference clear: apply
works on a column or row at a time, while map works on one cell
at a time.
$ python df_map.py
a b
0 10 40
1 20 50
2 30 60
transform for broadcasting
transform computes a value for each group but then
broadcasts it back: the returned object has the same shape as the
input, with the group result repeated on every row of that group.
Broadcasting simply means copying a summary so it lines up with the original
rows. This makes transform ideal for adding a new column.
import pandas as pd
df = pd.DataFrame({'group': ['A', 'A', 'B', 'B'], 'value': [10, 20, 30, 40]})
df['mean'] = df.groupby('group')['value'].transform('mean')
print(df)
The average of group A is 15 and the average of group B is 35.
transform repeats 15 twice and 35 twice, producing a Series of
length four that can be assigned straight to a new column. A plain
agg would return only two numbers and could not be assigned
this way. Grouping is explained in the
Pandas grouping data tutorial.
$ python df_transform.py group value mean 0 A 10 15.0 1 A 20 15.0 2 B 30 35.0 3 B 40 35.0
Prefer vectorized operations
When an operation is a plain arithmetic or string operation, Pandas can do
it vectorized: the work is pushed into fast compiled code instead
of calling a Python function once per element. Vectorized expressions are
both shorter and much faster than an equivalent apply call, so
they should be the first choice.
import pandas as pd
df = pd.DataFrame({'price': [10, 20, 30], 'qty': [2, 3, 4]})
print(df['price'] * df['qty'])
Multiplying two columns directly returns a Series with one result per row.
Compare it with df.apply(lambda row: row['price'] * row['qty'],
axis=1): both give the same numbers, but the vectorized version avoids
the per-row Python function calls. Reach for map,
apply or transform when the logic really is custom.
$ python vectorized.py 0 20 1 60 2 120 dtype: int64
Source
Pandas Series.map Documentation, Pandas DataFrame.apply Documentation, Pandas DataFrame.map Documentation, and Pandas DataFrame.transform Documentation
In this article we have compared map, apply and
transform and described the shape each one returns. For
summarising columns see Pandas aggregation,
and for changing how values are displayed see
Pandas formatting columns.
Author
List all Pandas tutorials.