生成 2 到 40 之间的随机数,均值为 20 作为 Pandas 中的一列 [英] generate a random number between 2 and 40 with mean 20 as a column in pandas

查看:60
本文介绍了生成 2 到 40 之间的随机数,均值为 20 作为 Pandas 中的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据框

I have a data frame as shown below

session      slot_num     appt_time
s1           1            2020-01-06 09:00:00
s1           2            2020-01-06 09:20:00
s1           3            2020-01-06 09:40:00
s1           3            2020-01-06 09:40:00
s1           4            2020-01-06 10:00:00
s1           4            2020-01-06 10:00:00
s2           1            2020-01-06 08:20:00
s2           2            2020-01-06 08:40:00
s2           2            2020-01-06 08:40:00
s2           3            2020-01-06 09:00:00
s2           4            2020-01-06 09:20:00
s2           5            2020-01-06 09:40:00
s2           5            2020-01-06 09:40:00
s2           6            2020-01-06 10:00:00
s3           1            2020-01-09 13:00:00
s3           1            2020-01-09 13:00:00
s3           2            2020-01-09 13:20:00
s3           3            2020-01-09 13:40:00

在上面我想添加一个名为 service_time 的列.

In the above I would like to add a column called service_time.

service_time 应包含 2 到 40 之间的任何随机数字,每个会话的平均值为 20.

service_time should contain any random digits between 2 to 40 with mean 20 for each session.

我更喜欢随机数应该遵循随机正态分布,均值为 20,标准差为 10,最小值为 2,最大值为 40

I prefer random numbers should follow random normal distribution with mean 20, standard deviation 10, minimum 2 and maximum 40

预期输出:

session      slot_num     appt_time               service_time
s1           1            2020-01-06 09:00:00     30
s1           2            2020-01-06 09:20:00     10
s1           3            2020-01-06 09:40:00     15
s1           3            2020-01-06 09:40:00     35
s1           4            2020-01-06 10:00:00     20
s1           4            2020-01-06 10:00:00     10
s2           1            2020-01-06 08:20:00     15
s2           2            2020-01-06 08:40:00     20
s2           2            2020-01-06 08:40:00     25
s2           3            2020-01-06 09:00:00     30
s2           4            2020-01-06 09:20:00     20
s2           5            2020-01-06 09:40:00     8
s2           5            2020-01-06 09:40:00     40
s2           6            2020-01-06 10:00:00     2
s3           1            2020-01-09 13:00:00     4
s3           1            2020-01-09 13:00:00     32
s3           2            2020-01-09 13:20:00     26
s3           3            2020-01-09 13:40:00     18

注意:请注意,这是遵循上述最小值、最大值和平均值标准的随机组合之一.

Note : please note that this is the one of that random combination which follows the minimum, maximum and mean criteria mentioned above.

推荐答案

一种可能的带有 cutom 功能的解决方案:

One possible solution with cutom function:

#https://stackoverflow.com/a/39435600/2901002
def gen_avg(n, expected_avg=20, a=2, b=40):
    while True:
        l = np.random.randint(a, b, size=n)
        avg = np.mean(l)

        if avg == expected_avg:
            return l

df['service_time'] = df.groupby('session')['session'].transform(lambda x: gen_avg(len(x)))
print (df)
   session  slot_num            appt_time  service_time
0       s1         1  2020-01-06 09:00:00            31
1       s1         2  2020-01-06 09:20:00             9
2       s1         3  2020-01-06 09:40:00            23
3       s1         3  2020-01-06 09:40:00            37
4       s1         4  2020-01-06 10:00:00             6
5       s1         4  2020-01-06 10:00:00            14
6       s2         1  2020-01-06 08:20:00            33
7       s2         2  2020-01-06 08:40:00            29
8       s2         2  2020-01-06 08:40:00            18
9       s2         3  2020-01-06 09:00:00            32
10      s2         4  2020-01-06 09:20:00             9
11      s2         5  2020-01-06 09:40:00            26
12      s2         5  2020-01-06 09:40:00            10
13      s2         6  2020-01-06 10:00:00             3
14      s3         1  2020-01-09 13:00:00            19
15      s3         1  2020-01-09 13:00:00            22
16      s3         2  2020-01-09 13:20:00             5
17      s3         3  2020-01-09 13:40:00            34

这篇关于生成 2 到 40 之间的随机数,均值为 20 作为 Pandas 中的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆