2016-01-08 38 views
0

我試圖從for循環中將數組的結果複製到數據框。然而,每一個我嘗試這一次,循環的最後的值在數據幀更新:使用數組輸出更新數據幀

counter = 0 
sample = [1,2,5,10,15,20,30,60,120,180,240,300,360,420,480,540,600] 
columns = ['1','2','5','10','15','20','30','60','120','180','240','300','360','420','480','540','600'] 

index = df.set_index([df.index]) 
resultsDf = pd.DataFrame(columns=columns) 
resultsDf = pd.DataFrame() 
resultsDf.set_index([resultsDf.index]) 

results = [] 
for index, rowEntry in TradesGTC.iterrows():#Entry of Trade 
    entryVolume = rowEntry[26] 
    entryPrice = rowEntry[28] 
    ccyPair = rowEntry[12][0:6]  
    entryTime = rowEntry['DateTime'] 
    for data in sample:   
     exitTime = entryTime + dt.timedelta(seconds = data) 
     f = MD.between_time(entryTime,exitTime) 
     buy = entryVolume > 0 
     sell = entryVolume < 0 
     if buy == True: 
      maxBidinTimeFrame = f['bid'].max()    
      profit = (maxBidinTimeFrame - entryPrice) * entryVolume 
      results.append(profit) 
     if sell == True: 
      minAskinTimeFrame = f['offer'].min() 
      profit = (entryPrice - minAskinTimeFrame) * entryVolume 
      results.append(profit) 
     resultsDf.append(results) 

被返回的輸出是:

resultsDf 

1 2 5 10 15 20 30 60 120 180 240 300 360 420 480 540 600 

我希望有列標題的數據幀 1 2 5 10 .... 600 ,結果在每列中下降,從而列出..

1 2 3 5 10....600 
100 200 100...-50 
. 
. 

欣賞所有幫助 感謝

+0

馬上我看到你覆蓋'你循環的底部,而不是追加新列df'。另外,問題的格式正確嗎?你的第一個'for'循環('TradesGTC'的循環)沒有縮進。 – chrisaycock

+0

我很好奇爲什麼'resultsDf'是'resultsDf = pd.DataFrame(columns = columns)',那麼在你將它定義爲'resultsDf = pd.DataFrame()'後立即。 – Jarad

+0

男士感謝您的期待 @chrisaycock感謝格式是因爲複製過去到stackoverflow。 我已經更新了附加結果的問題。但我知道列標題,但沒有結果。 – noidea

回答

0

感謝所有,但在拼圖缺少的一塊..

resultsDf = resultsDf.append(pd.DataFrame(data = results), ignore_index=True)