pandas.core.groupby.DataFrameGroupBy.apply#
- DataFrameGroupBy.apply(func, *args, include_groups=True, **kwargs)[source]#
Apply function
funcgroup-wise and combine the results together.The function passed to
applymust take a dataframe as its first argument and return a DataFrame, Series or scalar.applywill then take care of combining the results back together into a single dataframe or series.applyis therefore a highly flexible grouping method.While
applyis a very flexible method, its downside is that using it can be quite a bit slower than using more specific methods likeaggortransform. Pandas offers a wide range of method that will be much faster than usingapplyfor their specific purposes, so try to use them before reaching forapply.- Parameters:
- funccallable
A callable that takes a dataframe as its first argument, and returns a dataframe, a series or a scalar. In addition the callable may take positional and keyword arguments.
- include_groupsbool, default True
When True, will attempt to apply
functo the groupings in the case that they are columns of the DataFrame. If this raises a TypeError, the result will be computed with the groupings excluded. When False, the groupings will be excluded when applyingfunc.New in version 2.2.0.
Deprecated since version 2.2.0: Setting include_groups to True is deprecated. Only the value False will be allowed in a future version of pandas.
- args, kwargstuple and dict
Optional positional and keyword arguments to pass to
func.
- Returns:
- Series or DataFrame
See also
pipeApply function to the full GroupBy object instead of to each group.
aggregateApply aggregate function to the GroupBy object.
transformApply function column-by-column to the GroupBy object.
Series.applyApply a function to a Series.
DataFrame.applyApply a function to each row or column of a DataFrame.
Notes
Changed in version 1.3.0: The resulting dtype will reflect the return value of the passed
func, see the examples below.Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See Mutating with User Defined Function (UDF) methods for more details.
Examples
>>> df = pd.DataFrame({'A': 'a a b'.split(), ... 'B': [1,2,3], ... 'C': [4,6,5]}) >>> g1 = df.groupby('A', group_keys=False) >>> g2 = df.groupby('A', group_keys=True)
Notice that
g1andg2have two groups,aandb, and only differ in theirgroup_keysargument. Calling apply in various ways, we can get different grouping results:Example 1: below the function passed to apply takes a DataFrame as its argument and returns a DataFrame. apply combines the result for each group together into a new DataFrame:
>>> g1[['B', 'C']].apply(lambda x: x / x.sum()) B C 0 0.333333 0.4 1 0.666667 0.6 2 1.000000 1.0
In the above, the groups are not part of the index. We can have them included by using
g2wheregroup_keys=True:>>> g2[['B', 'C']].apply(lambda x: x / x.sum()) B C A a 0 0.333333 0.4 1 0.666667 0.6 b 2 1.000000 1.0
Example 2: The function passed to apply takes a DataFrame as its argument and returns a Series. apply combines the result for each group together into a new DataFrame.
Changed in version 1.3.0: The resulting dtype will reflect the return value of the passed
func.>>> g1[['B', 'C']].apply(lambda x: x.astype(float).max() - x.min()) B C A a 1.0 2.0 b 0.0 0.0
>>> g2[['B', 'C']].apply(lambda x: x.astype(float).max() - x.min()) B C A a 1.0 2.0 b 0.0 0.0
The
group_keysargument has no effect here because the result is not like-indexed (i.e. a transform) when compared to the input.Example 3: The function passed to apply takes a DataFrame as its argument and returns a scalar. apply combines the result for each group together into a Series, including setting the index as appropriate:
>>> g1.apply(lambda x: x.C.max() - x.B.min(), include_groups=False) A a 5 b 2 dtype: int64