Python Forum
append elements into the empty dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append elements into the empty dataframe
#1
def backtest(inCapital):
    rCapital = inCapital  
    qty1 = 0
    hp = 0
    
    cap=0
    preAdjClose = 0
    tempAdjClose = 0
    
    columns = ['Date'] #, 'AdjClose', 'POS', 'SMA' ,'LMA']
    df = pdd.DataFrame(columns=columns)
 

    for i in range(len(data)):
        
        
      
        if hp == 0:
            if data['short_moving_emavg'][i] > data['long_moving_emavg'][i]:
                hp = 1

                tempAdjClose = preAdjClose

                preAdjClose = data['Adj Close'][i]
               
                pd = data['Adj Close'][i] - preAdjClose
 
               
                qty=100
                pnl = qty * pd
               
                rCapital = rCapital + pnl
              

                print(data['Date'][i] +' | '+ str(data['Adj Close'][i]) +' | '+ str(tempAdjClose) +' | '+ str(hp) +' | '+
                      str(data['short_moving_emavg'][i]) +' | '+  str(data['long_moving_emavg'][i])+' | '+ str(pd)+' | '+ 
                      str(qty) +' | '+ str(pnl) +' | '+str(rCapital) ) 
                continue
Output:
1998-01-02 | 0.5143449999999999 | 0 | 1 | 0.4658776299999999 | 0.4653830649999999 | 0.0 | 100 | 0.0 | 100.0
I want to convert this output into dataframe. I tried appending elements but did not work
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting Elements via parameters pointing to those elements. rpalmer 3 628 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Add a row to a dataframe or append whole dataframe. tsurubaso 1 382 Jan-07-2021, 01:53 AM
Last Post: tsurubaso
  Append 2d empty lists NMMST 2 526 Oct-19-2020, 09:25 PM
Last Post: NMMST
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 771 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append list to empty array SchroedingersLion 1 933 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  Append to dataframe with for loop nocoffeenoworkee 0 11,518 May-01-2017, 11:44 AM
Last Post: nocoffeenoworkee
  Append to dataframe with for loop nocoffeenoworkee 2 4,329 Apr-27-2017, 06:55 PM
Last Post: nocoffeenoworkee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020