after groupby
, i want to agg
with lambda
with condition,
select only 'product1_Gift0' == 1
but doesn't seem able to get the answer
need guidance on the 'margin' calculation, instead of calculate all, calculate only when '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]]
thanks