2017-01-05 102 views
0

我試圖把每一行的總和我的熊貓數據幀:在熊貓數據幀總結行返回NAN

new_df['cash_change'] = new_df.sum(axis=0)

但是我的成績回頭率NaN

我想可能有當我將職位轉換爲乘號的小數點

pos_to_dec = np.array([Decimal(d) for d in security.signals['positions'].values])

我需要做的就是將我的列乘以一起。我將它轉換回不過:

cash_change[security.symbol] = cash_change[security.symbol].astype(float)

下面是完整的方法。其目的是進行一些列的乘法每個安全然後總和總在最後:

def get_cash_change(self): 
    """ 
    Calculate daily cash to be transacted every day. Cash change depends on 
    the position (either buy or sell) multiplied by the adjusted closing price 
    of the equity multiplied by the trade amount. 
    :return: 
    """ 
    cash_change = pd.DataFrame(index=self.positions.index) 
    try: 

     for security in self.market_on_close_securities: 
      # First convert all the positions from floating-point to decimals 
      pos_to_dec = np.array([Decimal(d) for d in security.signals['positions'].values]) 

      cash_change['positions'] = pos_to_dec 
      cash_change['bars'] = security.bars['adj_close_price'].values 

      # Perform calculation for cash change 
      cash_change[security.symbol] = cash_change['positions'] * cash_change['bars'] * self.trade_amount 

      cash_change[security.symbol] = cash_change[security.symbol].astype(float) 

      # Clean up for next security 
      cash_change.drop('positions', axis=1, inplace=True) 
      cash_change.drop('bars', axis=1, inplace=True) 

    except InvalidOperation as e : 
     print("Invalid input : " + str(e)) 

    # Sum each equities change in cash 
    new_df = cash_change.dropna() 

    new_df['cash_change'] = new_df.sum(axis=0) 

    return cash_change 

new_df數據幀最終看起來是這樣的:

   MTD  ESS  SIG  SNA cash_change 
price_date             
2000-01-04  0.0  0.00  0.00  0.00   NaN 
2000-01-05  0.0  0.00  0.00  0.00   NaN 
2000-01-06  0.0  0.00  0.00  0.00   NaN 
2000-01-07  0.0  0.00  0.00  0.00   NaN 
2000-01-10  0.0  0.00  0.00  0.00   NaN 
2000-01-11  0.0  0.00  0.00  0.00   NaN 
2000-01-12  0.0  0.00  0.00  0.00   NaN 
2000-01-13  0.0  0.00  0.00  0.00   NaN 
2000-01-14  0.0  0.00  0.00  0.00   NaN 
2000-01-18  0.0  0.00  0.00  0.00   NaN 
2000-01-19  0.0  0.00  0.00  0.00   NaN 
2000-01-20  0.0  0.00  0.00  0.00   NaN 
2000-01-21  0.0  0.00  0.00  0.00   NaN 
2000-01-24  0.0 1747.83 1446.71  0.00   NaN 
2000-01-25 3419.0  0.00  0.00  0.00   NaN 
2000-01-26  0.0  0.00  0.00 1660.38   NaN 
2000-01-27  0.0  0.00 -1293.27  0.00   NaN 
2000-01-28  0.0  0.00  0.00  0.00   NaN 

任何建議上我做錯了什麼?或者可能另一種方法來總結每一行的列?

+0

我建議添加最小可重現的示例 - http://stackoverflow.com/help/mcve。這裏的人通常願意幫忙,但我懷疑很多SO用戶會用你當前的問題玩這個猜謎遊戲 –

回答

3

當您在DF.sum方法中提供axis=0時,它會沿着索引執行求和(如果容易理解,則執行垂直方向)。因此,您只能計算出與數據框的4列對應的4個值。然後,您將此結果分配給數據框的新列。由於它們不共享相同的索引軸以重新索引,因此您會得到一系列NaN元素。

你實際上想要跨列(水平方向)進行求和。

更改該行:

new_df['cash_change'] = new_df.sum(axis=1) # sum row-wise across each column 

現在你會得到有限的計算彙總值。

+1

感謝您的幫助! – user3547551

1
new_df['cash_change'] = new_df.sum(axis=1) 
+0

這是OP最初嘗試導致「NaNs」出現的原因。 –

+0

@nickil maveli OP試試axis = 0 – Mahesh

+0

我可能知道這與我發佈的答案有什麼不同嗎?如果您有與此相關的內容,請修改您的帖子。 –