【发布时间】:2019-09-29 18:57:34
【问题描述】:
我已经看过this question,我知道numpy.random.choice,但我的问题略有不同。
鉴于此,我有一个如下数据集:
dict ={"Number of polyps":[10,8,3,1,2,6,13],
"Right ":[3,2,3,1,0,3,3],
"Left":[2,2,4,15,6,7,1] }
dt = pd.DataFrame(dict)
原来如此:
Number of polyps Right Left
10 3 2
8 2 2
3 3 4
1 1 15
2 0 6
6 3 7
13 3 1
我需要按照以下要求重新填写Right 和Left 列
-
Right和Left之和等于Number of polyps -
Right和Left的值来自它们当前值的加权概率
例如,对于给定的行如下:
Number of polyps Right Left
10 3 2
所以,对于这一行,它可能如下所示。这里0.6= 3/(3+2)和0.4= 2/(3+2):
nr = np.random.choice(["Right","Left"],size=10, replace=True,p=[0.6,0.4])
rightCount = list.count('Right')
leftCount = list.count('Left')
print(rightCount)
print(leftCount)
更新此行后将是:
Number of polyps Right Left
10 3 7
问题是,我必须对数据集中的所有行都这样做,但我不知道该怎么做!
【问题讨论】:
-
如果它适用于一行,将其放入 for 循环将使其适用于所有行...?
标签: python pandas numpy probability uniform-distribution