Apply function ``func`` group-wise and combine the results together. The function passed to ``apply`` must take a dataframe as its first argument and return a DataFrame, Series or scalar. ``apply`` will then take care of combining the results back together into a si
(self, func, *args, include_groups: bool = False, **kwargs)
| 1485 | # apply/agg/transform |
| 1486 | |
| 1487 | def apply(self, func, *args, include_groups: bool = False, **kwargs) -> NDFrameT: |
| 1488 | """ |
| 1489 | Apply function ``func`` group-wise and combine the results together. |
| 1490 | |
| 1491 | The function passed to ``apply`` must take a dataframe as its first |
| 1492 | argument and return a DataFrame, Series or scalar. ``apply`` will |
| 1493 | then take care of combining the results back together into a single |
| 1494 | dataframe or series. ``apply`` is therefore a highly flexible |
| 1495 | grouping method. |
| 1496 | |
| 1497 | While ``apply`` is a very flexible method, its downside is that |
| 1498 | using it can be quite a bit slower than using more specific methods |
| 1499 | like ``agg`` or ``transform``. Pandas offers a wide range of method that will |
| 1500 | be much faster than using ``apply`` for their specific purposes, so try to |
| 1501 | use them before reaching for ``apply``. |
| 1502 | |
| 1503 | Parameters |
| 1504 | ---------- |
| 1505 | func : callable |
| 1506 | A callable that takes a dataframe as its first argument, and |
| 1507 | returns a dataframe, a series or a scalar. In addition the |
| 1508 | callable may take positional and keyword arguments. |
| 1509 | |
| 1510 | *args : tuple |
| 1511 | Optional positional arguments to pass to ``func``. |
| 1512 | |
| 1513 | include_groups : bool, default False |
| 1514 | When True, will attempt to apply ``func`` to the groupings in |
| 1515 | the case that they are columns of the DataFrame. If this raises a |
| 1516 | TypeError, the result will be computed with the groupings excluded. |
| 1517 | When False, the groupings will be excluded when applying ``func``. |
| 1518 | |
| 1519 | .. versionadded:: 2.2.0 |
| 1520 | |
| 1521 | .. versionchanged:: 3.0.0 |
| 1522 | |
| 1523 | The default changed from True to False, and True is no longer allowed. |
| 1524 | |
| 1525 | **kwargs : dict |
| 1526 | Optional keyword arguments to pass to ``func``. |
| 1527 | |
| 1528 | Returns |
| 1529 | ------- |
| 1530 | Series or DataFrame |
| 1531 | A pandas object with the result of applying ``func`` to each group. |
| 1532 | |
| 1533 | See Also |
| 1534 | -------- |
| 1535 | pipe : Apply function to the full GroupBy object instead of to each |
| 1536 | group. |
| 1537 | aggregate : Apply aggregate function to the GroupBy object. |
| 1538 | transform : Apply function column-by-column to the GroupBy object. |
| 1539 | Series.apply : Apply a function to a Series. |
| 1540 | DataFrame.apply : Apply a function to each row or column of a DataFrame. |
| 1541 | |
| 1542 | Notes |
| 1543 | ----- |
| 1544 | The resulting dtype will reflect the return value of the passed ``func``, |
no test coverage detected