将值随机分配给python中的行 Series.sample

问题描述

我有以下输入表 (y):

参数1 参数2
1 12
2 23
3 66
4 98
5 90
6 14
7 7
8 56
9 1

我想从 A1 到 A9 随机分配值。输出表应如下所示:

参数1 参数2 参数3
1 12 A5
2 23 A2
3 66 A4
4 98 A8
5 90 A3
6 14 A7
7 7 A1
8 56 A9
9 1 A6
n = 9

TGn = round(len(y)/n)
idx = set(y.index // TGn)

y = y.apply(lambda x: x.sample(frac=1,random_state=1234)).reset_index(drop=True)
    
treatment_groups = [f"A{i}" for i in range(1,n+1)]
y['groupAfterRandomization'] = (y.index // TGn).map(dict(zip(idx,treatment_groups)))

我无法填充它打印为 NaN 的第一行值。我该如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)