I have already seen this question and I know numpy.random.choice
, but my question is slightly different.
Given that, I have a dataset as below:
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)
so, it is:
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
I need to refill the Right
and Left
column by below requirement
- Sum of
Right
andLeft
is equal toNumber of polyps
- The values of
Right
andLeft
comes from weighted probability of their current value
For example, for a given row as below:
Number of polyps Right Left
10 3 2
so, for this row it could be as below. Here 0.6= 3/(3+2)
and 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)
After updating this row will be:
Number of polyps Right Left
10 3 7
The problem is, I must do it for all the rows in dataset, but I am not sure how to do it!