Column-wise calculations
Compute per-column summaries such as totals, ranges, or custom statistics.
Pandas · Function
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.
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.
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), by_row='compat', engine=None, engine_kwargs=None, **kwargs)| Name | Type | Description |
|---|---|---|
func | function | Function to apply to each column or row. |
axis | {0 or 'index', 1 or 'columns'}, default 0 | Axis to process: 0 or 'index' for columns; 1 or 'columns' for rows. Default is 0. |
raw | bool, default False | Whether to pass ndarray objects instead of Series objects. Default is False. |
result_type | {'expand', 'reduce', 'broadcast', None}, default None | How to interpret list-like row results: 'expand', 'reduce', 'broadcast', or None. Default is None. |
args | tuple | Additional positional arguments passed to func. |
by_row | False, 'compat', default 'compat' | Controls row-wise compatibility handling for list-like or dict-like callables. Default is 'compat'. |
engine | {None, 'python', 'numba'}, default None | Execution engine selection. Default is None. |
engine_kwargs | dict, None | Additional keyword arguments for the selected engine. |
**kwargs | Any | Additional keyword arguments passed to func. |
Series or DataFrame: Result of applying func along the given axis of the DataFrame.
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.
Compute per-column summaries such as totals, ranges, or custom statistics.
Derive a value from multiple fields in each record.
Turn each row's list-like result into columns or retain the original shape.
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.
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.
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.mappandas.DataFrame.aggpandas.DataFrame.transformpandas.Series.apply