Python Forum

Full Version: FOR loop not working properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the below code.
I am trying to populate a new dataframe withan iterative for loop, but while i can add the "Year" for each iteration, i am not able to find the way to add the "tpl".
That should change at every interaction, but as you can see from the df, it only add the first row, like if it gets kicked out from the loop moving forward...
I noticed that 'mask' is also now allowing to show the dataframe (actually if i keep the line with 'mas" it only show an empty dataframe, while if i remove it it shows everything but i lose the timeframe i need.
Thanks for your help in advance.
I am interested in adding the tpl at each increase of K so that for each YEAR i have a corresponding TPL.
Thanks a lot.
# Total P/L

new_df_perf_sim = new_df

for k in range(0, 18):
    aaaa_start = 2002+k
    aaaa_end = aaaa_start+1
    mm = 1
    dd = 1
    nn = mm  
    aaaas = str(aaaa_start)
    aaaae = str(aaaa_end)
    mmb = str(mm)
    ddb = str(dd)
    nnb = str(nn)
    ap = '\''
    startdate = ap+aaaas+'-'+mmb+'-'+ddb+ap
    enddate = ap+aaaae+'-'+nnb+'-'+ddb+ap
    mask = (new_df_perf_sim['Date[i]'] >= startdate) & (new_df_perf_sim['Date[i]'] < enddate)
    new_df_perf_sim = new_df_perf_sim.loc[mask]
    tpl = new_df_perf_sim['Profit/Loss'].sum()
    tpl2 = new_df_perf_sim['Profit/Loss-2'].sum()
    perf_sim.loc[k, 'Year'] = aaaa_start
    perf_sim.loc[k, 'Tpl'] = new_df_perf_sim['Profit/Loss'].sum()
#    print(tpl)
#    print(tpl2)

RESULT: Year Tpl 0 2002.0 -2080.0 1 2003.0 0.0 2 2004.0 0.0 3 2005.0 0.0 4 2006.0 0.0