Skip to main content
 首页 » 编程设计

python之无法根据要求的百分比将**每天**个人分配到 3 组之10%/45%/45%

2024年11月24日11lexus

我想根据固定的每日百分比将现有数据集中的个体随机分配到 3 个不同的组。下面是示例数据集:

 Date               Customer_ID 
 1. 1/3/2019         411 
 2. 1/3/2019         414 
 3. 1/3/2019         421 
 4. 5/3/2019         431 
 5. 5/3/2019         433 
 6. 5/3/2019         441 
 7. 6/3/2019         442 
 8. 6/3/2019         443 
 9. 6/3/2019         444 

我使用下面的 Python 代码来创建组。虽然总体流量百分比是正确的,但未根据每天 所需的百分比正确分配组。

Group   % 
 A    10% 
 B    45% 
 C    45% 
 
              Expected outcome               Actual outcome 
 Date      Group A  Group B Group C     Group A Group B Group C 
  1/3/2019  10%      45%    45%           7%    2%       91% 
  1/4/2019  10%      45%    45%           12%   25%      63% 
  1/5/2019  10%      45%    45%           15%   50%      35% 
  1/6/2019  10%      45%    45%           20%   61%      19% 
  1/7/2019  10%      45%    45%           2%    7%       91% 
  1/8/2019  10%      45%    45%           1%    12%      87% 
  1/9/2019  10%      45%    45%           9%    21%      70% 
  1/10/2019 10%      45%    45%           13%   25%      62% 
  Overall   10%      45%    45%           10%   45%      45% 

当前代码:

# Create 3 different groups that have traffic assigned 10%/45%/45% 
df['Groups'] = df.groupby('Date')['Customer_ID']\ 
.transform(lambda x: np.random.choice(['Group_A', 'Group_B', 'Group_C'], 
                                      len(x),  p= [0.1,0.45,0.45])) 

该代码仅在整个数据集上提供所需的输出,而不是每天(如实际结果表所示)

根据每天所需的分配,我可以使用哪个 python 代码创建三个组?

请您参考如下方法:

好吧,看来我一开始就误解了这个问题(漫长的一天)。恕我直言,您的代码按预期工作(抱歉,我只是生成数字而不是日期):

import pandas as pd 
import numpy as np 
rows = 10000 
dates = np.random.choice(range(10), size = rows) 
Customer_IDs = np.random.choice(range(2*rows), size = rows, replace = False) 
data = np.vstack([dates, Customer_IDs]).T 
 
df = pd.DataFrame(data, columns = ["Date", "Customer_ID"]) 
 
df['Groups'] = df.groupby('Date')['Customer_ID']\ 
    .transform(lambda x: np.random.choice(['Group_A', 'Group_B', 'Group_C'], 
                                      len(x),  p= [0.1,0.45,0.45])) 
 
print(df.groupby(['Date','Groups']).agg({'Date':'count'})\ 
    .groupby(level = 0).apply(lambda x:100 * x / float(x.sum())) ) 

现在,可能会有一些随机性,严格来说 10/45/45 是不太可能的。

我的建议是手动检查每个特定日期的分布情况,并与“实际”表进行比较:

from collections import Counter 
test_date = 1 # change this to '1/3/2019' for example 
cntr = Counter(df[df["Date"]==test_date]["Groups"]) 
cntr_sum = sum(cntr.values()) 
print( {k: np.round(100 * v/cntr_sum, 2) 
            for k,v in cntr.items()} ) 

附言。希望你会有这样的东西:

{'Group_B': 43.35, 'Group_C': 46.23, 'Group_A': 10.42} 

希望这次我做对了!