Dears,
I want to write a function for the below codes but I am unable to write.
path = 'C:/Users/user/Desktop/'
a = pd.read_csv(path+'benchmark1.csv')
b = a.loc[:,['RIC','Weight']]
b['RIC'] = b.RIC.str.replace('.SE','').astype(int)
Bench_1 = b.rename(columns={'RIC':'Ticker','Weight':'Weight_1'})
Data1 = pd.merge(Data,Bench_1, on= 'Ticker', how= 'left')
Data1 = Data1.fillna(0)
Data1['Weight_1'] = round(Data1.Weight_1*100,2)
c = pd.read_csv(path+'benchmark2.csv')
d = c.loc[:,['RIC','Weight']]
d['RIC'] = d.RIC.str.replace('.SE','').astype(int)
Bench_2 = d.rename(columns={'RIC':'Ticker','Weight':'Weight_2'})
Data2 = pd.merge(Data1,Bench_2, on= 'Ticker', how= 'left')
Data2 = Data2.fillna(0)
Data2['Weight_2'] = round(Data2.Weight_2*100,2)
I tried to write a below function but I got two issue: 1-how to put multiple benchmark in bench. 2- how to segregate weight_1 and weight_2.
def data_sort(bench):
a = pd.read_csv(path+'bench.csv')
b = a.loc[:,['RIC','Weight']]
b['RIC'] = b.RIC.str.replace('.SE','').astype(int)
Bench_1 = b.rename(columns={'RIC':'Ticker','Weight':'Weight_1'})
Data1 = pd.merge(Data,Bench_1, on= 'Ticker', how= 'left')
Data1 = Data1.fillna(0)
Data1['Weight_1'] = round(Data1.Weight_1*100,2)
return Data1
Appreciate your help.