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``). By default (``result_type=None``), the final return type is i
(
self,
func: AggFuncType,
axis: Axis = 0,
raw: bool = False,
result_type: Literal["expand", "reduce", "broadcast"] | None = None,
args=(),
by_row: Literal[False, "compat"] = "compat",
engine: Callable | None | Literal["python", "numba"] = None,
engine_kwargs: dict[str, bool] | None = None,
**kwargs,
)
| 12200 | return result |
| 12201 | |
| 12202 | def apply( |
| 12203 | self, |
| 12204 | func: AggFuncType, |
| 12205 | axis: Axis = 0, |
| 12206 | raw: bool = False, |
| 12207 | result_type: Literal["expand", "reduce", "broadcast"] | None = None, |
| 12208 | args=(), |
| 12209 | by_row: Literal[False, "compat"] = "compat", |
| 12210 | engine: Callable | None | Literal["python", "numba"] = None, |
| 12211 | engine_kwargs: dict[str, bool] | None = None, |
| 12212 | **kwargs, |
| 12213 | ): |
| 12214 | """ |
| 12215 | Apply a function along an axis of the DataFrame. |
| 12216 | |
| 12217 | Objects passed to the function are Series objects whose index is |
| 12218 | either the DataFrame's index (``axis=0``) or the DataFrame's columns |
| 12219 | (``axis=1``). By default (``result_type=None``), the final return type |
| 12220 | is inferred from the return type of the applied function. Otherwise, |
| 12221 | it depends on the `result_type` argument. The return type of the applied |
| 12222 | function is inferred based on the first computed result obtained after |
| 12223 | applying the function to a Series object. |
| 12224 | |
| 12225 | Parameters |
| 12226 | ---------- |
| 12227 | func : function |
| 12228 | Function to apply to each column or row. |
| 12229 | axis : {0 or 'index', 1 or 'columns'}, default 0 |
| 12230 | Axis along which the function is applied: |
| 12231 | |
| 12232 | * 0 or 'index': apply function to each column. |
| 12233 | * 1 or 'columns': apply function to each row. |
| 12234 | |
| 12235 | raw : bool, default False |
| 12236 | Determines if row or column is passed as a Series or ndarray object: |
| 12237 | |
| 12238 | * ``False`` : passes each row or column as a Series to the |
| 12239 | function. |
| 12240 | * ``True`` : the passed function will receive ndarray objects |
| 12241 | instead. |
| 12242 | If you are just applying a NumPy reduction function this will |
| 12243 | achieve much better performance. |
| 12244 | |
| 12245 | .. note:: |
| 12246 | |
| 12247 | When ``raw=True``, the result dtype is inferred from the **first** |
| 12248 | returned value. |
| 12249 | |
| 12250 | result_type : {'expand', 'reduce', 'broadcast', None}, default None |
| 12251 | These only act when ``axis=1`` (columns): |
| 12252 | |
| 12253 | * 'expand' : list-like results will be turned into columns. |
| 12254 | * 'reduce' : returns a Series if possible rather than expanding |
| 12255 | list-like results. This is the opposite of 'expand'. |
| 12256 | * 'broadcast' : results will be broadcast to the original shape |
| 12257 | of the DataFrame, the original index and columns will be |
| 12258 | retained. |
| 12259 |