I have main raw data of securities, out of which I need to create multiple portfolios of securities based on certain filtering criterias. I am used to working in C++, not very clear on how the below can be implemented in python.
I tried to make different dataframes using nested for-loops:
i - used to loop through years from 2007 to 2017 (column yr in raw data)
j - used to loop through regions from 1 to 4 (column Region in raw data)
for i in range (2007, 2018):
for j in range (1,5):
dfij_filter = (df['yr'] == i) & (df['Region'] == j)
dfij = dfij[dfij_filter]
dfij = dfij.join(dfco.groupby('ISSUER_NAME')['E_SCORE'].mean(), on = 'ISSUER_NAME', rsuffix = '_ry')
dfij = dfij.join(dfco.groupby('ISSUER_NAME')['P_SCORE'].mean(), on = 'ISSUER_NAME', rsuffix = '_ry')
dfij = dfij.join(dfco.groupby('ISSUER_NAME')['Q_SCORE'].mean(), on = 'ISSUER_NAME', rsuffix = '_ry')
dfij = dfij.drop_duplicates(subset['ISSUER_NAME'], keep=False)
dfij_E = dfij.sort_values('E_SCORE_ry', ascending = False)
dfij_ETOP = dfij_E.iloc[:50, :]
dfij_P = dfij.sort_values('P_SCORE_ry', ascending = False)
dfij_PTOP = dfij_P.iloc[:50, :]
dfij_Q = dfij.sort_values('E_SCORE_ry', ascending = False)
dfij_QTOP = dfij_Q.iloc[:50, :]
I need to create different dataframes and then apply few functions on those dataframes: Essentially the flow is: Step 1: Yr filter --> Step 2: Region filter --> Step 3: Calc an average E score value, avg P score value, avg Q score value for that yr and region --> (E, P, Q are different columns) Step 4: Arrange the securities in descending order of average E score --> Step 5: Pick top 50 securities and put them in a dataframe
Repeat Step 4 and 5 for P and Q scores as well.
Essentially creating 10*4*3 dataframes.
These dataframes can then be used for backtesting purposes
Any help would be greatly appreciated. Thanks