-1

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.

Arfeen Zia
  • 81
  • 1
  • 8

1 Answers1

0

This should work

def data_sort(benchmark,Data,label):
    a = pd.read_csv(path+benchmark)
    b = a.loc[:,['RIC','Weight']]
    b['RIC'] = b.RIC.str.replace('.SE','').astype(int)
    bench = b.rename(columns={'RIC':'Ticker','Weight':label})
    Data1 = pd.merge(Data,bench, on= 'Ticker', how= 'left')
    Data1 = Data1.fillna(0)
    Data1[label] = round(getattr(Data1, label)*100,2)
    return Data1
Data1=data_sort('benchmark1.csv',Data,'Weight_1')
Data2=data_sort('benchmark2.csv',Data1,'Weight_2')
Nirvedh Meshram
  • 439
  • 6
  • 13
  • Thank you so much. I run the code but it showing the below error: return Data1[label] = round(Data1.Weight_1*100,2) invalid syntax – Arfeen Zia Dec 23 '18 at 22:52
  • 1
    Hi Afreen, I edited to fix that. Note that since you have not provided a minimum verifiable code with expected output. It is difficult to provide exact help as i cant run this code on my machine. – Nirvedh Meshram Dec 23 '18 at 22:59
  • Hi, could you please give me your email. I will send you my full code if you don't mind – Arfeen Zia Dec 23 '18 at 23:01
  • it's working perfect, now I am changing the label with respect to exact Fund Weight like Weight_ABC and thinking to change from Data1[label] = round(Data1.Weight_1*100,2) to Data1[label] = round(Data1.label*100,2) – Arfeen Zia Dec 23 '18 at 23:13
  • oh yes thats how it should be I will edit the answer – Nirvedh Meshram Dec 23 '18 at 23:14
  • There is an error 'DataFrame' object has no attribute 'label' – Arfeen Zia Dec 23 '18 at 23:17
  • I think you need something like this question we want the attribute from the given label am I correct? https://stackoverflow.com/questions/2612610/how-to-access-object-attribute-given-string-corresponding-to-name-of-that-attrib – Nirvedh Meshram Dec 23 '18 at 23:20
  • Thank you so much for your help bro. I really do appreciate it. its working now – Arfeen Zia Dec 23 '18 at 23:53