mercredi 4 janvier 2017

Create list of randomly selected items based on criteria

I'm trying to randomly select a list of funds based on their investment strategy. For instance if I have the following dataframe:

FundName/Strategy

Fund1/Activist
Fund2/Activist
Fund3/Activist
Fund4/Activist

Fund5/Macro
Fund6/Macro
Fund7/Macro

I would like to randomly select 2 funds from each strategy. I have 18 strategies and thousands of funds.

I tried the following:

 Strategylist={'Activist', 'Macro'}

 for x in Strategylist:
     np.random.choice(df[df.loc[:,'Strategy']==x].FundName,2,replace=False)

Ideally, the number of fund selected from each strategy would be flexible. For instance if:

Strategy/# of funds

Activist/2

Macro/1

Everytime, I run the loop I would get a unique random selection of funds with 2 random activist funds and 1 random macro fund such as Fund1, Fund3, Fund6




Aucun commentaire:

Enregistrer un commentaire