pandas.core.groupby.GroupBy.pipe#
- GroupBy.pipe(func, *args, **kwargs)[源代码]#
应用函数 func 将参数传递给此GroupBy对象并返回函数的结果。
使用 .pipe 当您希望通过将预期Series、DataFrames、GroupBy或ResSamer对象的函数链接在一起来提高可读性时。与其写作,不如
>>> h(g(f(df.groupby('group')), arg1=a), arg2=b, arg3=c)
你可以写
>>> (df.groupby('group') ... .pipe(f) ... .pipe(g, arg1=a) ... .pipe(h, arg2=b, arg3=c))
它的可读性更强。
- 参数
- func(可调用,字符串)的可调用或元组
函数应用于此GroupBy对象,或者也可以应用于 (callable, data_keyword) 元组所在位置 data_keyword 是一个字符串,它指示 callable 它需要GroupBy对象。
- args可迭代,可选
传入的位置参数 func 。
- kwargsDICT,可选
传入的关键字参数词典 func 。
- 退货
- 对象 :的返回类型 func 。的返回类型
参见
Series.pipe
将带参数的函数应用于系列。
DataFrame.pipe
将带参数的函数应用于数据帧。
apply
将函数应用于每个组,而不是整个GroupBy对象。
注意事项
见更多 here
示例
>>> 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 4
要在一次传递中获得每组最大值和最小值之间的差异,您可以执行以下操作
>>> df.groupby('A').pipe(lambda x: x.max() - x.min()) B A a 2 b 2