2016-05-02 51 views
-3

我有一個名爲stockData數據框的操作(請參閱下面的。頭()):循環通過一個數據幀的列上執行特定的命名列

stockData    BBG.XLON.BTA.S_LAST BBG.XLON.BTA.S_VOLUME BBG.XLON.BTA.S_MKTCAP \ 
date                    
2008-01-04    265.00    38773846    21184.6422 
2008-01-07    264.00    68107684    21104.7001 
2008-01-08    277.75    113809490    22184.2614 
2008-01-09    272.00    47890229    21723.9135 
2008-01-10    276.75    40391750    22102.3023 

      BBG.XLON.BTA.S_VWAP BBG.XLON.BTA.S_CLOSE \ 
date              
2008-01-04    268.4860     NaN 
2008-01-07    263.6428     NaN 
2008-01-08    272.2632     NaN 
2008-01-09    271.4513     NaN 
2008-01-10    275.3315     NaN 

      BBG.XLON.BTA.S_LAST_ADJ BBG.XLON.BTA.S_VOLUME_ADJ \ 
date                
2008-01-04      NaN      NaN 
2008-01-07      NaN      NaN 
2008-01-08      NaN      NaN 
2008-01-09      NaN      NaN 
2008-01-10      NaN      NaN 

      BBG.XLON.BTA.S_EXCHANGE_HOLIDAY BBG.XLON.BTA.S_CORP_ACTION \ 
date                  
2008-01-04        NaN       NaN 
2008-01-07        NaN       NaN 
2008-01-08        NaN       NaN 
2008-01-09        NaN       NaN 
2008-01-10        NaN       NaN 

      BBG.XLON.BTA.S_REPORTING   ...    \ 
date           ...    
2008-01-04      NaN   ...    
2008-01-07      NaN   ...    
2008-01-08      NaN   ...    
2008-01-09      NaN   ...    
2008-01-10      NaN   ...    

      BBG.XLON.VOD.S_LAST_ADJ BBG.XLON.VOD.S_VOLUME_ADJ \ 
date                
2008-01-04      NaN      NaN 
2008-01-07      NaN      NaN 
2008-01-08      NaN      NaN 
2008-01-09      NaN      NaN 
2008-01-10      NaN      NaN 

      BBG.XLON.VOD.S_EXCHANGE_HOLIDAY BBG.XLON.VOD.S_CORP_ACTION \ 
date                  
2008-01-04        NaN       NaN 
2008-01-07        NaN       NaN 
2008-01-08        NaN       NaN 
2008-01-09        NaN       NaN 
2008-01-10        NaN       NaN 

      BBG.XLON.VOD.S_REPORTING BBG.XLON.VOD.S_FX \ 
date              
2008-01-04      NaN    NaN 
2008-01-07      NaN    NaN 
2008-01-08      NaN    NaN 
2008-01-09      NaN    NaN 
2008-01-10      NaN    NaN 

      BBG.XLON.VOD.S_LAST_EUR BBG.XLON.VOD.S_MKTCAP_EUR \ 
date                
2008-01-04      NaN      NaN 
2008-01-07      NaN      NaN 
2008-01-08      NaN      NaN 
2008-01-09      NaN      NaN 
2008-01-10      NaN      NaN 

      BBG.XLON.VOD.S_VWAP_EUR BBG.XLON.VOD.S_CLOSE_EUR 
date               
2008-01-04      NaN      NaN 
2008-01-07      NaN      NaN 
2008-01-08      NaN      NaN 
2008-01-09      NaN      NaN 
2008-01-10      NaN      NaN 

我通過每列試圖週期中在列名稱末尾有'_LAST'或'_VWAP'。在這些列上,我需要循環查看每個值。我試圖在下面的代碼中執行此操作,但無法找到stockData數據框的列標識符並將其設置爲變量'priceIdentifier'。

def putCorpActions(stockData,corpActionData): 


    for y, row in corpActionData.iterrows(): 

     identifier = row['unique_id'] 
     #corpDate = row['date'] 
     corpDate = y 
     factor_value = row['factor_value'] 
     reference = row['reference'] 
     factor= row['factor'] 



     for columns in stockData: 

       priceIdentifier = row[columns] 
       if priceIdentifier == '*_LAST' or priceIdentifier == '*_VWAP' 
        for z, row in stockData.iterrows(): 
         price = row[] 
         #cycle trough the column doing something 

有人可以請示我如何做到這一點,並通過期望的數據幀列循環。

非常感謝

+0

目前還不清楚你在做什麼。你可以說得更詳細點嗎?你當前的代碼是否給出錯誤信息? – IanS

回答

1
priceIdentifier == '*_LAST' or priceIdentifier == '*_VWAP' 

將無法​​工作。你可以使用「正則表達式」

import re # please import this 


def putCorpActions(stockData,corpActionData): 


    for y, row in corpActionData.iterrows(): 

     identifier = row['unique_id'] 
     #corpDate = row['date'] 
     corpDate = y 
     factor_value = row['factor_value'] 
     reference = row['reference'] 
     factor= row['factor'] 



     for columns in stockData: 

       priceIdentifier = row[columns] 
       rstr = r'.*_(LAST|VWAP)' 
       if re.match(rstr, priceIdentifier) # regular expression matching 
        for z, row in stockData.iterrows(): 
         price = row[] 
         #cycle trough the column doing something 
+0

或'endswith'如果你願意寫兩個條件而不是一個... – IanS

+0

謝謝piRSquared。我在行priceIdentifier = row [columns]上得到KeyError:'BBG.XLON.BTA.S_LAST'。任何想法如何解決?謝謝 – Stacey

+0

這就是說你試圖使用字符串'BBG.XLON.BTA.S_LAST'作爲字典或數據框中的鍵,但它不存在。我必須有更多的上下文。 – piRSquared

相關問題