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
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries i sawtooth500 3 385 Mar-22-2024, 03:08 AM
Last Post: deanhystad
  Using .append() with list vs dataframe Mark17 7 9,897 Jun-12-2022, 06:54 PM
Last Post: Mark17
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,549 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Add a row to a dataframe or append whole dataframe. tsurubaso 1 1,402 Jan-07-2021, 01:53 AM
Last Post: tsurubaso
  Append 2d empty lists NMMST 2 2,792 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 2,523 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append list to empty array SchroedingersLion 1 2,143 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  Append to dataframe with for loop nocoffeenoworkee 0 13,016 May-01-2017, 11:44 AM
Last Post: nocoffeenoworkee
  Append to dataframe with for loop nocoffeenoworkee 2 6,129 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