Hi I would like to create combinations of ID's. I know how to create all possible combinations but am stuck on one final part of the operation. Any help will be greatly appreciated.
I have a dataset as follows:
import pandas as pd from itertools import combinations_with_replacement
d1 = {'Subject': ['Subject1','Subject1','Subject1','Subject2','Subject2','Subject2','Subject3','Subject3','Subject3','Subject4','Subject4','Subject4','Subject5','Subject5','Subject5'],
'Actual':['1','0','0','0','0','1','0','1','0','0','0','0','1','0','1'],
'Event':['1','2','3','1','2','3','1','2','3','1','2','3','1','2','3'],
'Category':['1','1','2','1','1','2','2','2','2','1','1','1','1','2','1'],
'Variable1':['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15'],
'Variable2':['12','11','10','9','8','7','6','5','4','3','2','1','-1','-2','-3'],
'Variable3': ['-6','-5','-4','-3','-4','-3','-2','-1','0','1','2','3','4','5','6']}
d1 = pd.DataFrame(d1)
I want to create all possible combinations of the subjects within each event within each tier. This is done by (from a previous question Form groups of individuals python (pandas)):
L = [(i[0], i[1], y[0], y[1]) for i, x in d1.groupby(['Event','Category'])['Subject']
for y in list(combinations_with_replacement(x, 2))]
df = pd.DataFrame(L, columns=['Event','Category','Subject_IDcol1','Subject_IDcol2'])
Now, I want to take only those pairs for which Actual = 1 and randomly select "n" Subjects for which Actual = 0. Here for simplicity sake let's take n = 1. I want to run the function combinations_with_replacement on this new list.
The output that I want to get for example (assuming random selection) is something like this:
For event 1, category 1: Subject 1 and 5 have Actual = 1 and suppose Subject 2 is randomly drawn.
As compared to this, in the previous case, the result was something like this (for event =1 and category =1)
Any help will be appreciated. Thanks.
Aucun commentaire:
Enregistrer un commentaire