分组后如何在lambda中进行条件求和

时间:2019-09-04 09:13:27

标签: python lambda group-by aggregate

groupby之后,我想以agg为条件的lambda, 仅选择'product1_Gift0' == 1

但似乎无法获得答案

需要有关“保证金”计算的指导,而不是全部计算,仅在'product1_Gift0' equal '1'时计算

data = [['john', 'A01', 0, 0.0],['john', 'A01', 1, 1.0],['john', 'A01', 1, 0.5],['jess', 'B01', 0, 0.0],['jess', 'B01', 0, 0.0],['jess', 'B01', 1, 0.8]]


df2 = pd.DataFrame(data, columns = ['member', 'orderID','product1_Gift0','margin']) 

df3 = df2.groupby('member').agg({
                                 'product1_Gift0': lambda x: sum(x)/len(x),
                                 'margin' : lambda x: sum(x)/len(x),
                              })

actual_result = [['john', 'A01', 0.3333, 0.50],
                 ['john', 'A01', 0.6667, 0.27]]

expected_result = [['john', 'A01', 0.3333, 0.75],
                   ['john', 'A01', 0.6667, 0.80]]

谢谢

1 个答案:

答案 0 :(得分:0)

尝试这个:

>>> df2.groupby('member').apply(lambda x: pd.Series({"product_Gift0_mean": x.product1_Gift0.mean(), "margin_mean": sum(x.margin * (x.product1_Gift0==1))/(x.product1_Gift0==1).sum()}))
        margin_mean  product_Gift0_mean
member
jess           0.80            0.333333
john           0.75            0.666667