I am working on a homework problem where I am supposed to construct a portfolio of stocks based on certain constraints. Based on these constraints I will select my stocks in the portfolio and will conduct re-balancing of my portfolio based on these scores on a monthly basis. So I have two dataframes - one is the stock returns and the other is the scores on which I will pcik my stocks in the portfolio. Further, the data frame shape is 12x360, i.e. the dates are the index and the column headers are the stocks. I have a 360 stocks universe and have to pick top 30 and the bottom 30 based on the scores.
Since I am new to python, I am having difficulty in visualizing how I can pick stocks from a dataframe based on values in another dataframe. Secondly, is it possible to select the top 30 and bottom 30 scores across a single row, since my dates are the index of the data frame.
Below is the initial code I have prepared for cleaning and calculating the returns. If anybody can guide me on the next step, I will be very grateful.
import pandas as pd
import numpy as np
def log_return(price):
return np.log(price) - np.log(price.shift(1))
dataframe = pd.read_excel(r'M:\Overlay\09_Projects\Madhav\Trial.xlsx')
df1 = dataframe.iloc[:,0:14]
df1 = df1.transpose()
df1.columns = df1.iloc[0]
df1 = df1.drop(df1.index[[0,1]])
for cols in df1:
df1[cols] = pd.to_numeric(df1[cols])
df1_ret = log_return(df1)
df2 = dataframe
df2.drop(df2.iloc[:, 1:14], axis = 1, inplace=True)
df2 = df2.transpose()
df2.columns = df2.iloc[0]
df2 = df2.drop(df2.index[0])
for cols in df2:
df2[cols] = pd.to_numeric(df2[cols])