I have pandas dataframe, say df which looks like
Region ID
A 111
A 222
A 333
A 444
B 555
B 666
B 777
C 888
C 999
ID column has its weights. In this case, A's weight is 2, B's weight is 2 and C's weight is 1.
weights never are more than the number of values in "Region" column, meaning A's weight never be more than 4 as we have 4 records for A
I want to make a new column and in this column assign random integer values according to weights in ID column BUT these random values have to be equally distributed. For more clarity, I expect new dataframe should look like this
Region ID Random_Value
A 111 1
A 222 2
A 333 1
A 444 2
B 555 2
B 666 2
B 777 1
C 888 1
C 999 1
When the values in "Region" column is odd, like "B" I want to assign random values equally but the remainder can have any random integer value.
When the values in "Region" column is even, like "A" and its weight is 2 I need to assign random integer value from 1 to 2 inclusively and the number of these random integers should be equal.
I tried many ways but no success. Is there a way to solve this problem?
My code is the following:
df['Random_Value'] = np.nan
A = df['region'] == 'A'
df.loc[A, 'Random_Value'] = np.random.randint(1,3, size=A.sum())
[1, 2]right?[1,2,3]