如何防止Pandas Dataframe在每一行重复列名?

时间:2016-07-12 13:54:49

标签: python-2.7 pandas dataframe

我正在使用ExactTarget Saleforce营销云SOAP API进行工作。我已经获得了API来响应我的应用程序,响应是一个字典列表。像这样:

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 = "aaa@aaa.aaa"
   EventDate = 2016-07-12 00:40:17
   EventType = "aaaa"
   TriggeredSendDefinitionObjectID = None
   BatchID = 1
   URLID = 11111
   URL =

我已经成功迭代了这些项目,但作为我的代码中的测试,只撤回了3个值。 ID,PartnerKey,CreatedDate。

我已将变量提供给Pandas DataFrame并且已成功填充。我的问题和问题是列名在每个行级不断重复,左侧的行号都是零。

如何在使行号顺序排列的同时停止重复列名称?

代码:

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

提前谢谢。

1 个答案:

答案 0 :(得分: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