2016-07-12 105 views
1

我正在使用ExactTarget Saleforce營銷雲SOAP API進行工作。我已經獲得了API來響應我的應用程序,並且響應是一個字典列表。像這樣:如何防止Pandas Dataframe在每一行重複列名?

ClickEvent){ 
    Client = 
     (ClientID){ 
     ID = 111111 
     } 
    PartnerKey = None 
    CreatedDate = 2016-07-12 00:40:17 
    ModifiedDate = 2016-07-12 00:40:17 
    ID = 11111 
    ObjectID = "11111" 
    SendID = 111111 
    SubscriberKey = "[email protected]" 
    EventDate = 2016-07-12 00:40:17 
    EventType = "aaaa" 
    TriggeredSendDefinitionObjectID = None 
    BatchID = 1 
    URLID = 11111 
    URL = 

我已經成功地迭代了項目,但作爲一個測試在我的代碼只拉回3個值。 ID,PartnerKey,CreatedDate。

我已經將這些變量提供給熊貓數據框,並且它們已經成功填充。我的問題是列名在每一行都連續重複,左側的行號都是零。

如何避免列名重複使行號連續?

代碼:現在

import ET_Client 
import pandas as pd 
import json 



try: 
    debug = False 
    stubObj = ET_Client.ET_Client(False, debug) 

    ## Modify the date below to reduce the number of results returned from the request 
    ## Setting this too far in the past could result in a very large response size 
    retrieveDate = '2014-07-11T13:00:00.000' 

    #ET call for clicks 
    print '>>>ClickEvents' 
    getClickEvent = ET_Client.ET_ClickEvent() 
    getClickEvent.auth_stub = stubObj 
    getResponse = getClickEvent.get() 
    ResponseResults = getResponse.results 

    for ClickEvent in ResponseResults: 
     ID = str(ClickEvent['ID']) 
     PartnerKey = ClickEvent['PartnerKey'] 
     CreatedDate = ClickEvent['CreatedDate'] 





     df = pd.DataFrame({'ID':[ID],'PartnerKey':[PartnerKey], 'ClickEvent' :[CreatedDate]}) 

     print df 

輸出:

ClickEvent   ID   PartnerKey 
0 2016-05-21 10:41:46 111111111  None 
      ClickEvent   ID PartnerKey 
0 2016-05-21 11:29:25 111111111  None 
      ClickEvent   ID PartnerKey 

預先感謝您。

回答

3

您正在爲ResponseResults中的每個項目創建(並打印)一個數據框,而不是包含所有結果的單個數據框。嘗試這樣的:

ids = [] 
partner_keys = [] 
created_dates = [] 
for ClickEvent in ResponseResults: 
    ids.append(str(ClickEvent['ID'])) 
    partner_keys.append(ClickEvent['PartnerKey']) 
    created_dates.append(ClickEvent['CreatedDate']) 

df = pd.DataFrame({'ID': ids, 'PartnerKey': partner_keys, 
        'ClickEvent' : created_dates}) 
print df 
+0

工作很漂亮!非常感謝! – RustyShackleford