HOWTO · Pandas

How to Apply Function to Every Row in Pandas DataFrame

Learn how to apply a callable to every row in a Pandas DataFrame with axis=1, including lambda, NumPy, and user-defined functions with or without extra arguments.

On this page

Use DataFrame.apply() with axis=1 to pass each row to a callable. The callable can return a transformed row, which apply() combines into a DataFrame. This guide shows the row-axis choice, a lambda, a NumPy function, and user-defined functions with and without extra arguments.

Basic Syntax of Pandas apply() Function

The general form is dataframe.apply(function, axis=1, args=()). Set axis=1 when the callable should receive rows. The default is axis=0, which passes columns instead. For a row-wise call, the callable receives a Series whose index contains the column labels; its return value determines the result. Returning a scalar for every row produces a Series, while returning a Series with consistent labels produces a DataFrame.

args supplies additional positional arguments after the row. Use it when a custom callable needs a value that is the same for every row. For the full parameter reference, see DataFrame.apply().

Apply lambda Function to Each Row in Pandas DataFrame

A lambda is useful for a short transformation. This example adds 10 to each value in the row and returns a DataFrame with the same columns. For longer logic, a named function is easier to read and reuse.

Run this example:

import pandas as pd

dataframe = pd.DataFrame([(1, 34, 23), (11, 31, 11)], columns=list("ABC"))
result = dataframe.apply(lambda row: row + 10, axis=1)
print(result)
    A   B   C
0  11  44  33
1  21  41  21

Apply a NumPy Function to Each Row of Pandas DataFrame

Pass a NumPy callable directly when it operates on each value in the row. Here, np.sqrt transforms each numeric cell and apply() assembles the transformed rows. Square root is appropriate for nonnegative numeric values; choose a different operation when the data has other types or domain requirements.

Run this example:

import numpy as np
import pandas as pd

dataframe = pd.DataFrame([(4, 9, 16), (25, 36, 49)], columns=list("ABC"))
result = dataframe.apply(np.sqrt, axis=1)
print(result)
     A    B    C
0  2.0  3.0  4.0
1  5.0  6.0  7.0

Apply a User-Defined Function to Each Row of Pandas DataFrame With Arguments

Define the row transformation as a function when it needs more than a short expression. In the example, row is the first argument and 2 is supplied after it through args; the function doubles each value. args should be a tuple of positional arguments, so a single extra value is written as (2,).

Run this example:

import pandas as pd

def multiply_row(row, factor):
    return row * factor

dataframe = pd.DataFrame([(2, 3, 4), (3, 5, 10)], columns=list("ABC"))
result = dataframe.apply(multiply_row, axis=1, args=(2,))
print(result)
   A   B   C
0  4   6   8
1  6  10  20

Apply a User-Defined Function to Each Row of Pandas DataFrame Without Arguments

When the operation needs no shared parameter, define the callable with only the row parameter and pass it directly. This example multiplies each row value by four. The returned rows keep their labels, so the result is a DataFrame with the original columns.

Run this example:

import pandas as pd

def multiply_by_four(row):
    return row * 4

dataframe = pd.DataFrame([(2, 3, 4), (3, 5, 10)], columns=list("ABC"))
result = dataframe.apply(multiply_by_four, axis=1)
print(result)
    A   B   C
0   8  12  16
1  12  20  40

Use apply() when the operation needs a callable that works with a whole row. If the transformation is a straightforward column-wise arithmetic operation, a direct vectorized expression can be clearer and avoid per-row Python calls.