🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.apply()

AI & DATA SCIENCE // df-apply

df.apply() runs an arbitrary custom function over each row, each column, or each group of a DataFrame, offering maximum flexibility at the cost of speed compared to pandas' built-in vectorized methods.

Syntax

df.apply(func, axis=0)

Deep Dive Course

apply() with axis=0, the default, calls func once per column, passing each column as a Series; axis=1 instead calls func once per row, passing each row as a Series, which is the more common use for row-wise custom logic that can't be expressed with simple vectorized operations. Because apply() invokes a Python function once per row or column, it runs at Python speed rather than pandas' fast, vectorized C-level operations, which makes it noticeably slower than an equivalent vectorized expression whenever one is actually available.

1Understanding df.apply()

apply() with axis=0, the default, calls func once per column, passing each column as a Series; axis=1 instead calls func once per row, passing each row as a Series, which is the more common use for row-wise custom logic that can't be expressed with simple vectorized operations. Because apply() invokes a Python function once per row or column, it runs at Python speed rather than pandas' fast, vectorized C-level operations, which makes it noticeably slower than an equivalent vectorized expression whenever one is actually available.

💡

Before reaching for apply(), check whether the same logic can be expressed with a vectorized pandas/NumPy operation instead — apply() calls a Python function once per row, or column, which is dramatically slower than an equivalent built-in vectorized operation for anything beyond small datasets.

editor.html
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))
localhost:3000

2Practical Example

Here is a real-world application of df.apply() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3]})
print(df["a"].apply(lambda x: x ** 2))
localhost:3000

3Best Practices

Follow these guidelines when working with df.apply():

1. Prefer a vectorized operation, built-in methods, arithmetic, np.where(), etc., over apply() whenever one can express the same logic, for significantly better performance

2. Reserve apply() for genuinely custom, row-wise logic that can't be cleanly vectorized

3. Pass axis=1 explicitly for row-wise operations, since the default axis=0 applies the function per column instead, which is a common source of confusion

⚠️

Tip: Before reaching for apply(), check whether the same logic can be expressed with a vectorized pandas/NumPy operation instead — apply() calls a Python function once per row, or column, which is dramatically slower than an equivalent built-in vectorized operation for anything beyond small datasets.

editor.html
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))
localhost:3000

Examples

Example 01Basic Usage
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))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3]})
print(df["a"].apply(lambda x: x ** 2))

Best Practices

  • Prefer a vectorized operation, built-in methods, arithmetic, np.where(), etc., over apply() whenever one can express the same logic, for significantly better performance
  • Reserve apply() for genuinely custom, row-wise logic that can't be cleanly vectorized
  • Pass axis=1 explicitly for row-wise operations, since the default axis=0 applies the function per column instead, which is a common source of confusion

Interview Question

Why is adding two columns directly with df['a'] + df['b'] generally preferred over df.apply(lambda row: row['a'] + row['b'], axis=1)?

Hint: Think about how many times a Python function actually gets called in each approach.

Adding the columns directly is a vectorized operation — it delegates the entire addition to pandas/NumPy's underlying, pre-compiled C code, which processes the whole column at once without invoking the Python interpreter for each individual element. df.apply(..., axis=1) instead calls the given lambda function once for every single row, each call carrying real Python-level function-call overhead, which adds up significantly for large DataFrames. Since the vectorized addition achieves the exact same result without any of that per-row Python overhead, it's the clearly preferred approach whenever the logic is simple enough to express that way.

Exercises

MediumPractice using df.apply() in a real scenario.
View Solution
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))

Frequently Asked Questions

Why is adding two columns directly with df['a'] + df['b'] generally preferred over df.apply(lambda row: row['a'] + row['b'], axis=1)?

Adding the columns directly is a vectorized operation — it delegates the entire addition to pandas/NumPy's underlying, pre-compiled C code, which processes the whole column at once without invoking the Python interpreter for each individual element. df.apply(..., axis=1) instead calls the given lambda function once for every single row, each call carrying real Python-level function-call overhead, which adds up significantly for large DataFrames. Since the vectorized addition achieves the exact same result without any of that per-row Python overhead, it's the clearly preferred approach whenever the logic is simple enough to express that way.

Related Functions

df-transformdf-groupbylist-comprehensions