Group by apply
During our discussion on algorithms and how to apply them back in Chapter 5, Algorithms and How to Apply Them, we came across the Apply function, which is both powerful and terrifying at the same time. An equivalent function for group by exists as pd.core.groupby.DataFrameGroupBy.apply
with all of the same caveats. Generally, this function is overused, and you should opt for pd.core.groupby.DataFrameGroupBy.agg
or pd.core.groupby.DataFrameGroupBy.transform
instead. However, for the cases where you don’t really want an aggregation or a transformation, but something in between, using apply
is your only option.
Generally, pd.core.groupby.DataFrameGroupBy.apply
should only be used as a last resort. It can produce sometimes ambiguous behavior and is rather prone to breakage across releases of pandas.
How to do it
In the There’s more… section of the previous recipe, we mentioned how it is not possible to start with a pd.DataFrame
of the following...