2017-12-18 251 views
0

我想從我創建的數據框中插入數據,使用API​​調用到GA,並使用psycopg2將其插入到我的postgresql數據庫中。 這是我使用的代碼:psycopg2谷歌分析數據到postgresql

garesults = df.reindex_axis(['campaign', 'adClicks', 'adCost', 'CPC', 'sessions', 'bounceRate', 'pageviewsPerSession', 'goal6ConversionRate', 'goal6Completions', 'goal6Values'], axis = 1) 

gafill = garesults.fillna(value = 0) 


# Connect to an existing database 
conn = psycopg2.connect("dbname=test user=xxx password=xxx") 

# Open a cursor to perform database operations 
cur = conn.cursor() 

# Execute a command: this creates a new table 
#cur.execute("CREATE TABLE adform (campaign, campaignid, impressions, clicks);") 
for row in gafill:  
    cur.execute("""INSERT INTO ga (campaign, adClicks, adCost, CPC, sessions, bounceRate, pageviewsPerSession, goal6ConversionRate, goal6Completions, goal6Values) 
         VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", [row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]]) 

當我運行這個它帶回了一條錯誤消息:

IndexError: string index out of range 

有人能看到我在做什麼錯?

+0

註釋掉表創建語句不符合插入語句。我不確定這是問題還是僅僅是將問題編輯成問題的人爲因素。 –

回答

0

我認爲問題在於如何嘗試遍歷數據框。

使用try itertuples代替:

for index, row in gafill.itertuples(): .....