I am using pandas mask/where function with random() as the callable - is it possible to get this to calculate a different value for each entry so that i dont get the same random number in each mask cell or can I only do this using apply?
e.g.
df_factor_history = pd.DataFrame([
['2020-10-01', 'A', 1, 5, 3],
['2020-10-01', 'B', 1, 5, 3],
['2020-11-01', 'A', 1, 5, 3],
['2020-11-01', 'B', 1, 5, 3],
['2020-12-01', 'A', 1, 5, 3],
['2020-12-01', 'B', 1, 5, 3],
],
columns=['as_at_date',
'factor_name',
'observation_1',
'observation_2',
'observation_3'])
df_factor_history.set_index(['as_at_date', 'factor_name'], inplace=True)
df_factor_history
df_valid = pd.DataFrame([
['2020-09-01', True, True],
['2020-10-01', False, True],
['2020-11-01', True, False],
['2020-12-01', True, True],
['2021-01-01', True, True]],
columns = ['as_at_date', 'A', 'B'])
df_valid.set_index(['as_at_date'], inplace=True)
df_valid
from random import seed
from random import random
# seed random number generator
seed(1)
df_factor_history.where(df_valid.T.unstack(), random())
This generates the same random number (in this case 0.134364) and i would like, in this example, six different random numbers


