On this page
pandas.core.groupby.GroupBy.pipe
GroupBy.pipe(self, func, *args, **kwargs)[source]-
Apply a function
funcwith arguments to this GroupBy object and return the function’s result.New in version 0.21.0.
Use
.pipewhen you want to improve readability by chaining together functions that expect Series, DataFrames, GroupBy or Resampler objects. Instead of writing>>> h(g(f(df.groupby('group')), arg1=a), arg2=b, arg3=c)You can write
>>> (df.groupby('group') ... .pipe(f) ... .pipe(g, arg1=a) ... .pipe(h, arg2=b, arg3=c))which is much more readable.
Parameters: -
func : callable or tuple of (callable, string) -
Function to apply to this GroupBy object or, alternatively, a
(callable, data_keyword)tuple wheredata_keywordis a string indicating the keyword ofcallablethat expects the GroupBy object. -
args : iterable, optional -
positional arguments passed into
func. -
kwargs : dict, optional -
a dictionary of keyword arguments passed into
func.
Returns: -
object : the return type of func.
See also
Series.pipe- Apply a function with arguments to a series.
DataFrame.pipe- Apply a function with arguments to a dataframe.
apply- Apply function to each group instead of to the full GroupBy object.
Notes
See more here
Examples
>>> df = pd.DataFrame({'A': 'a b a b'.split(), 'B': [1, 2, 3, 4]}) >>> df A B 0 a 1 1 b 2 2 a 3 3 b 4To get the difference between each groups maximum and minimum value in one pass, you can do
>>> df.groupby('A').pipe(lambda x: x.max() - x.min()) B A a 2 b 2 -
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.core.groupby.GroupBy.pipe.html