0
0
PandasHow-ToBeginner · 3 min read

How to Use apply in pandas: Syntax, Examples, and Tips

In pandas, apply lets you run a custom function across rows or columns of a DataFrame or elements of a Series. Use df.apply(func, axis=0) to apply func to each column, or axis=1 to apply it to each row.
📐

Syntax

The apply function in pandas is used like this:

  • df.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs)

func: The function you want to apply to each row or column.
axis: 0 to apply to each column, 1 to apply to each row.
raw: If True, passes ndarray instead of Series to the function.
result_type: Controls the type of the final output.
args and kwargs: Additional arguments for the function.

python
df.apply(func, axis=0)
💻

Example

This example shows how to use apply to calculate the range (max - min) for each row in a DataFrame.

python
import pandas as pd

data = {'A': [1, 4, 3], 'B': [4, 2, 5], 'C': [7, 1, 6]}
df = pd.DataFrame(data)

# Define a function to calculate range
def row_range(row):
    return row.max() - row.min()

# Apply function to each row
range_per_row = df.apply(row_range, axis=1)
print(range_per_row)
Output
0 6 1 3 2 3 dtype: int64
⚠️

Common Pitfalls

Common mistakes when using apply include:

  • Using the wrong axis value, which changes whether the function applies to rows or columns.
  • Writing functions that do not handle Series input properly.
  • Expecting apply to be very fast; it can be slower than vectorized operations.

Example of a wrong and right way:

python
import pandas as pd

data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)

# Wrong: axis=0 applies function to columns, but function expects row
 def add_one(row):
     return row + 1

print(df.apply(add_one, axis=0))  # Works here but changes columns

# Right: use axis=1 to apply to rows
print(df.apply(add_one, axis=1))
Output
A B 0 2 4 1 3 5 A B 0 2 4 1 3 5
📊

Quick Reference

ParameterDescription
funcFunction to apply to each row or column
axis0 for columns, 1 for rows
rawIf True, passes ndarray instead of Series
result_typeControls output type: 'expand', 'reduce', or 'broadcast'
argsPositional arguments to pass to func
kwargsKeyword arguments to pass to func

Key Takeaways

Use df.apply(func, axis=1) to apply a function to each row and axis=0 for each column.
The function passed to apply should accept a pandas Series when axis is set.
apply is flexible but can be slower than built-in vectorized pandas functions.
Check the axis parameter carefully to avoid unexpected results.
Use additional args and kwargs to pass extra parameters to your function.