Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 FOR loop not working properly
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
ichabod801 wrote Sep-07-2019, 11:58 AM:
Post all code, output and errors (in it's entirety) between their respective tags. Here are instructions. This is the third time you have been told to do this. Start doing it yourself.

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)