HOWTO · Pandas

How to Apply Lambda Function to Pandas DataFrame

This guide explains how to apply Lambda functions to pandas DataFrame using DataFrame.assign() and DataFrame.apply() methods.

On this page

Use a Python lambda with pandas when a short, single-expression function fits the task: use DataFrame.assign() to create columns, DataFrame.apply() for row- or column-wise calculations, Series.map() to transform values, and a boolean mask to filter rows. The operation and its axis determine what the lambda receives and what it returns.

What a lambda function does in pandas

A Python lambda expression creates a small anonymous function containing one expression, such as lambda value: value * 2. It describes a calculation; the pandas operation receiving that function determines whether it sees a whole column, a row, or an individual Series value. Use a named def function when the logic needs multiple statements, reuse, or a clearer name.

Add derived columns with DataFrame.assign()

DataFrame.assign() returns a DataFrame with the requested new or replaced columns, leaving the original DataFrame unchanged. A callable assignment receives the DataFrame, so one expression can use one or several existing columns. For example, df.assign(percentage=lambda data: data["marks"] / 1000 * 100) derives a percentage, while df.assign(total=lambda data: data["math"] + data["science"]) combines two columns. The new column aligns with the DataFrame’s index.

Apply a function to rows or columns with DataFrame.apply()

DataFrame.apply() passes each column by default: axis=0 applies the function to columns, while axis=1 applies it to each row. For a row-wise calculation such as adding a row’s base and bonus values, write df.apply(lambda row: row["base"] + row["bonus"], axis=1). The returned object depends on what the function returns; a scalar per row produces a Series. For simple arithmetic on a column, prefer vectorized pandas operations, which are usually clearer and faster than calling Python once per row.

Map or filter values in a Series

Python’s built-in map(function, values) transforms each input and returns an iterator; converting it with list(...) materializes the results. Series.map(function) is the pandas counterpart and returns a Series aligned to the original index. For example, df["age"].map(lambda age: age + 1) creates incremented values without changing the DataFrame until assigned.

Python’s built-in filter(predicate, values) keeps input values for which the predicate is true and returns an iterator. When filtering a pandas DataFrame, a boolean mask is more direct and retains matching rows and their other columns: df[df["age"] < 25]. A Series can also be filtered with df["age"][df["age"] < 25]. These are different outcomes: filter over a Series yields values, whereas a DataFrame mask selects complete rows.

Apply conditional logic with a lambda

A conditional expression fits inside a lambda’s single expression: lambda income: "Stable" if income >= 5000 else "Unstable". Applying it to a Series creates one label per income; the threshold is inclusive, so 5000 receives Stable, and lower values receive Unstable. For a whole DataFrame, assign the result back to a column, for example df["category"] = df["income"].apply(lambda income: "Stable" if income >= 5000 else "Unstable").

Choose an approach and check the result

Use assign() to add columns, apply() when a function must consume each row or column, Series.map() to transform each value, and a boolean mask to select matching rows. Check the axis, the input passed to the lambda, and whether the result is a Series or DataFrame before assigning it. This self-contained example demonstrates these patterns with related data:

import pandas as pd

df = pd.DataFrame({"name": ["Ari", "Bea", "Cal"], "math": [80, 92, 75], "science": [85, 88, 90], "income": [4000, 6000, 5000]})
result = df.assign(
    total=lambda data: data["math"] + data["science"],
    next_income=lambda data: data["income"].map(lambda value: value + 1000),
    category=lambda data: data["income"].apply(lambda value: "Stable" if value >= 5000 else "Unstable"),
)
selected = result[result["total"] >= 170]
print(selected[["name", "total", "next_income", "category"]].to_string(index=False))