Skip to content
Justice Tech School

Apply and Map

map and apply run a function on each value. Prefer built in vectorized operations when they exist because they are much faster.

Example
import pandas as pd
import numpy as np
s = pd.Series([1, 2, 3, 4])
print(s.map(lambda x: x ** 2).tolist())
print(s.map({1: 'one', 2: 'two'}).tolist())
print(s.apply(lambda x: x + 100).tolist())

Result

[1, 4, 9, 16]
['one', 'two', nan, nan]
[101, 102, 103, 104]

Exercise

Square each item.

s.(lambda x: x * x)

Quiz Test

Which is faster?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.