bpd.DataFrame.apply

DataFrame.apply(func, axis=0)[source]

Apply a function along an axis of the DataFrame.

Objects passed to the function are Series objects whose index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1). The final return type is inferred from the return type of the applied function.

Parameters:
  • func (function) – Function to apply to each column or row.
  • axis ({0 or 'index', 1 or 'columns'}, default 0) –

    Axis along which the function is applied:

    • 0 or ‘index’: apply function to each column.
    • 1 or ‘columns’: apply function to each row.
Returns:

applied – Result of applying func along the given axis of the DataFrame.

Return type:

Series or DataFrame

Examples

>>> def add_two(row):
...     return row + 2
>>> df = bpd.DataFrame(A=[1, 1],
...                    B=[2, 2])
>>> df.apply(add_two)
   A  B
0  3  4
1  3  4