mardi 9 avril 2019

Assign random values equally to pandas dataframe

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.

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 amount of these random integers should be equal.

I tried many ways but no success. Is there a way to solve this problem?




Aucun commentaire:

Enregistrer un commentaire