Applying Functions
Transform columns element-by-element with apply, map and vectorized methods.
Syntax
df['col'].apply(func)When a built-in operation is not enough, apply and map run your own function over the data.
When to reach for what
- Prefer vectorized operations (
df['x'] * 2) — fastest. - Use
Series.mapor.applyfor custom per-element logic. - Use
.strmethods for text columns.
Example
import pandas as pd
df = pd.DataFrame({'name': ['ann', 'bob'], 'age': [34, 29]})
# Custom function per element
df['bucket'] = df['age'].apply(lambda a: 'senior' if a >= 30 else 'junior')
# Vectorized string method
df['name'] = df['name'].str.title()
print(df)
# name age bucket
# 0 Ann 34 senior
# 1 Bob 29 juniorWhen to use it
- A data scientist uses apply with a lambda to parse and standardise inconsistently formatted phone numbers in a text column.
- An ML engineer applies a custom bucketing function to an age column to convert it to ordinal categories before one-hot encoding.
- A data pipeline uses df.apply(func, axis=1) to compute a row-level risk score derived from multiple columns simultaneously.
More examples
Apply a lambda to a column
Applies a lowercase lambda to every element of the text column, creating a new normalised column.
import pandas as pd
df = pd.DataFrame({'text': ['Hello World', 'PYTHON ai', 'Data Science']})
df['lower'] = df['text'].apply(lambda x: x.lower())
print(df)Apply a custom function row-wise
Applies a multi-column function row by row with axis=1 to compute a risk label from each row's income and debt values.
import pandas as pd
df = pd.DataFrame({'income': [30000, 75000, 120000], 'debt': [5000, 20000, 8000]})
def risk_score(row):
ratio = row['debt'] / row['income']
return 'high' if ratio > 0.3 else 'low'
df['risk'] = df.apply(risk_score, axis=1)
print(df)Vectorised str methods vs apply
Shows that pandas str accessor methods are faster for simple string ops, while apply is reserved for logic that cannot be expressed as a built-in method.
import pandas as pd
df = pd.DataFrame({'name': [' Alice ', 'BOB', 'carol ']})
# Prefer vectorised str methods over apply for strings
df['clean'] = df['name'].str.strip().str.title()
print(df)
# apply is needed for complex logic that str methods cannot express
df['initial'] = df['clean'].apply(lambda x: x[0])
print(df)
Discussion