Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Function

df.apply

Apply a callable independently along each DataFrame column or row. The default passes labeled Series objects and infers the result shape from the callable; options support ndarray inputs and explicit row-wise list-result handling.

How it works

DataFrame.apply runs a callable across columns or rows. By default it passes each selected axis as a Series and infers the output structure from the callable's result; options can instead pass arrays or control list-like row results.

Syntax

DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), by_row='compat', engine=None, engine_kwargs=None, **kwargs)

Parameters

NameTypeDescription
funcfunctionFunction to apply to each column or row.
axis{0 or 'index', 1 or 'columns'}, default 0Axis to process: 0 or 'index' for columns; 1 or 'columns' for rows. Default is 0.
rawbool, default FalseWhether to pass ndarray objects instead of Series objects. Default is False.
result_type{'expand', 'reduce', 'broadcast', None}, default NoneHow to interpret list-like row results: 'expand', 'reduce', 'broadcast', or None. Default is None.
argstupleAdditional positional arguments passed to func.
by_rowFalse, 'compat', default 'compat'Controls row-wise compatibility handling for list-like or dict-like callables. Default is 'compat'.
engine{None, 'python', 'numba'}, default NoneExecution engine selection. Default is None.
engine_kwargsdict, NoneAdditional keyword arguments for the selected engine.
**kwargsAnyAdditional keyword arguments passed to func.

Returns

Series or DataFrame: Result of applying func along the given axis of the DataFrame.

Key points

  • func is the callable invoked for every selected column or row.
  • The default axis=0 processes columns; axis='columns' or axis=1 processes rows.
  • With raw=False, each input is a labeled Series, while raw=True supplies an ndarray.
  • For row-wise application, result_type='expand' converts list-like outputs into separate columns.
  • For row-wise list-like results, reduce favors a Series instead of expanding them into columns.
  • The broadcast mode keeps the DataFrame's original shape, index, and columns when compatible.

Common mistakes

  • result_type has an effect only for row-wise application; setting it while applying by column does not change the result.
  • When outputs vary across rows or columns, the first result can determine the inferred result structure, so return consistently shaped values.
  • Do not assume a DataFrame or Series result; inspect the callable's output and choose result_type when row-wise shape needs to be explicit.

Example: Apply a function to columns and rows

import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [10, 20]})
column_totals = df.apply(sum)
row_totals = df.apply(sum, axis="columns")
print(column_totals.to_dict())
print(row_totals.tolist())

Applies the callable once to each column by default, then once to each row when the column axis is selected.

When to use it

Column-wise calculations

Compute per-column summaries such as totals, ranges, or custom statistics.

Row-wise feature creation

Derive a value from multiple fields in each record.

Controlling row-result shape

Turn each row's list-like result into columns or retain the original shape.

Interview questions

QWhat does DataFrame.apply do, and what is the default axis?

It applies a function along one DataFrame axis and returns a Series or DataFrame. With axis=0, the function receives each column; with axis=1, it receives each row.

Follow-up: How would you preserve the original shape for row-wise list-like output? Use result_type='broadcast', which broadcasts results to the original DataFrame shape and retains its index and columns.

QHow do raw and result_type affect apply?

raw=False is the default, so the callable receives each row or column as a Series. With raw=True, it receives an ndarray instead, which can avoid Series-specific overhead.

Follow-up: When does result_type matter? It controls list-like results only for axis=1, with expand, reduce, or broadcast determining the resulting shape.

QWhich arguments let you configure the callable and execution?

The callable can be passed positional arguments through args and keyword arguments through kwargs. The by_row option controls how list-like or dict-like callables are handled when applying row-wise compatibility behavior.

Follow-up: What does the engine option represent? The current signature allows an engine selection and engine_kwargs for execution-specific configuration; consult the documented engine choices before relying on them.

  • pandas.DataFrame.map
  • pandas.DataFrame.agg
  • pandas.DataFrame.transform
  • pandas.Series.apply

Source: pandas.DataFrame.apply. Checked 2026-10-15.