I have a list of Ids (device Ids) in a DataFrame. I want to assign randomly A or B to each one of these devices (split them into two halfs):
Assume we have a DataFrame named devices with a column "DeviceId" and 9364957 rows.
Option 1:
def coin():
p = 0.5
r = np.random.random()
return 'B' if r > p else 'A'
devices['Experiment'] = pd.DataFrame( [coin() for i in range(devices.shape[0])])
g = devices.groupby(['Experiment']).agg(['count'])
print(g.head(10))
Output :
Experiment
A 4681923B 4683034
there are 1,111 entries more in A than in B!
Option 2: (i got stuck :()
A = devices.sample(frac=0.5, replace=False)
print('\tselect as A: ', A.shape[0])
select as A: 4682478
in a simple calculation this is much better split because this way B will get 4682479 (the delta between both is exactly 1)
But how can i proceed from here?
My target is to get an updated DataFrame devices with two columns: DeviceId , Experiment (which is either "A" or "B"
Aucun commentaire:
Enregistrer un commentaire