我已经看过这个问题并且我知道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
andLeft
列
- 和等于
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
问题是,我必须对数据集中的所有行都这样做,但我不知道该怎么做!