I have a dataframe with over a million rows. For every row I have 4 columns, containing the weights. How can I efficiently sample for every row with their corresponding weights? I would just like to choose a number 1,2,3 or 4 for every row using the weights of each row. Right now I have this for-loop but that will take way too long.
df = pd.DataFrame({
'1': [0.155, 0.138, ...],
'2': [0.473, 0.307, ...],
'3': [0.291, 0.490, ...],
'4': [0.080, 0.064, ...],
'pick': ['']
})
for i in range(0, len(df)):
df['pick'][i] = random.choices([1,2,3,4], weights=[df['1'][i], df['2'][i], df['3'][i], df['4'][i]], k=1)