在关于GroupBy对象的新
我可以看出DataFrame对象中的管道功能与应用功能的区别,但对于GroupBy对象却不清楚。有人能解释一下或者给出一些使用管道功能而不能使用应用功能的GroupBy的例子吗?
.pipe()
方法的pandas文档示例中,接受相同lambda的.apply()
方法将返回相同的结果。In [195]: import numpy as np
In [196]: n = 1000
In [197]: df = pd.DataFrame({'Store': np.random.choice(['Store_1', 'Store_2'], n),
.....: 'Product': np.random.choice(['Product_1', 'Product_2', 'Product_3'], n),
.....: 'Revenue': (np.random.random(n)*50+10).round(2),
.....: 'Quantity': np.random.randint(1, 10, size=n)})
In [199]: (df.groupby(['Store', 'Product'])
.....: .pipe(lambda grp: grp.Revenue.sum()/grp.Quantity.sum())
.....: .unstack().round(2))
Out[199]:
Product Product_1 Product_2 Product_3
Store
Store_1 6.93 6.82 7.15
Store_2 6.69 6.64 6.77
我可以看出DataFrame对象中的管道功能与应用功能的区别,但对于GroupBy对象却不清楚。有人能解释一下或者给出一些使用管道功能而不能使用应用功能的GroupBy的例子吗?