vendredi 3 novembre 2017

Conditional random allocation in Python

I need to conditionally randomly allocate users to groups. The table governing the process is as follows:

   A  B  C
0  9  1  1
1  1  7  8
2  0  2  1

According to the above matrix, there's a total of 11 users from area 0, 16 from area 1, and 3 from area 2.

Furthermore, of the 11 users from area 0, 9 should be allocated to group A, 1 each should be allocated to B and C. The process is analogous for the rest of the groups.

I have some code in Python:

import random
import pandas as pd
df = pd.DataFrame({"A": [9,1,0], "B": [1,7,2], "C": [1,8,1]})
random.sample(range(1,df.sum(axis=1)[0] + 1),df.sum(axis=1)[0])

The last line creates a random vector of integers e.g. [1, 4, 10, 2, 5, 11, 9, 3, 8, 7, 6]. I can allocate indices from 1 to 9 to group A, the index with 10 to group B, the index with 11 to group C. In other words, user 3 goes to group B, user 6 goes to group C, and all the rest go to group A.

How can I automate the process I described in words above? (the actual allocation matrix is 10 x 10)




Aucun commentaire:

Enregistrer un commentaire