如何根据每个组的大小进行分组然后再加权值

时间:2019-04-25 12:15:03

标签: python pandas transform pandas-groupby

我想在完成交易后按比例分配给每位员工。因此,我首先需要总结导致销售的每个客户的联系数量,然后将奖励分配给参与此过程的每个员工。

import pandas as pd
df = pd.DataFrame({"Cust_ID":[1,1,1,2,3,3], "Employee": ["A","B","B","C","B","A"], "Purchase":[0,0,1,1,0,1]})

df
Cust_ID Employee  Purchase
0        1        A         0
1        1        B         0
2        1        B         1
3        2        C         1
4        3        B         0
5        3        A         1

当最终销售(Cust_ID = 1)需要3个(或更多)步骤时,奖励应按50%,30%和20%(0%..)分配。 对于2个步骤,分别为70%和30%。一步= 100%

结果应如下所示:

   Cust_ID Employee  Purchase  Reward
0        1        A         0     0.2
1        1        B         0     0.3
2        1        B         1     0.5
3        2        C         1     1.0
4        3        B         0     0.3
5        3        A         1     0.7

我尝试使用df["Reward"] = df.groupby("Cust_ID").Purchase.transform("xxx"),但这没有执行分布式奖励。

谢谢!

1 个答案:

答案 0 :(得分:2)

首先让我们扩充DataFrame:

df['Touch'] = df.groupby('Cust_ID').cumcount()
df['Touches'] = df.groupby('Cust_ID').Employee.count()[df.Cust_ID].values
df['Reward'] = 0.0

现在我们有了基本设置:

   Cust_ID Employee  Purchase  Touch  Touches  Reward
0        1        A         0      0        3     0.0
1        1        B         0      1        3     0.0
2        1        B         1      2        3     0.0
3        2        C         1      0        1     0.0
4        3        B         0      0        2     0.0
5        3        A         1      1        2     0.0

最后,应用奖励规则:

df.loc[df.Touches == 1, 'Reward'] = 1.0
df.loc[(df.Touches == 2) & (df.Touch == 0), 'Reward'] = 0.3
df.loc[(df.Touches == 2) & (df.Touch == 1), 'Reward'] = 0.7
df.loc[(df.Touches == 3) & (df.Touch == 0), 'Reward'] = 0.2
df.loc[(df.Touches == 3) & (df.Touch == 1), 'Reward'] = 0.3
df.loc[(df.Touches == 3) & (df.Touch == 2), 'Reward'] = 0.5

使用np.select()可以更巧妙地完成最后一部分。这是给读者的练习。