2017-10-05 151 views
-1
tlist = [("a1", "a2","a3"),("b1", "b2","b3"),("c1", "c2","c3")] 

我想要什麼:追加列表

df=pd.DataFrame([["a1","a2","a3"],["b1","b2","b3"],["c1","c2","c3"]]) 

我可以這樣做:

df2 = pd.DataFrame(tlist, columns=['col1', 'col2', 'col3']) 

然而,元組的列表是從一些數據庫中提取等等我有一個循環,並做它一次拉數據一塊,然後追加。

最新最好的方式做到這一點?

的數據拉可高達一個十億行,現在,它可以生長。

謝謝。

#very big table# 
sql2 = "Select col1,col2,col3 from bigT" 
#very big table# 
try: 
    cursor.execute (sql2) 
except cx_Oracle.DatabaseError: 
    print ('Failed \n'+sql2) 

#need to do it in chunk as not enough memory and blow up! 
while True: 
    tlist = cursor.fetchmany() 
    print(type(tlist)) 
    print (len(tlist)) 
    if rows == []: 
     break; 
    #I cannot get this one to work 
    df.append([tlist],ignore_index=True) 
    #I cannot get this one to work 
+1

我不明白。 – piRSquared

+0

請澄清,這篇文章不清楚 –

+0

請看我添加的僞代碼。 – Mookayama

回答

0

我發現一個更容易的解決方案!

import pandas as pd 
print(con.version) 
query = """select * from all_tab_columns""" 
df_ora = pd.read_sql(query, con=con) 
0
import cx_Oracle 



conn_str="scott/[email protected](DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db1.org.waaw.com)(PORT=1234))(CONNECT_DATA=(SERVICE_NAME=hatsx)))" 


con = cx_Oracle.connect(conn_str) 
cursor= con.cursor() 
cursor.arraysize = 10000 

import pandas as pd 
rowsx=[("xxxxxxxxxx","xxxxxxxxxx","xxxxxxxxxx","xxxxxxxxxx","xxxxxxxxxx")] 
labels=['col1', 'col2','col3'] 
df = pd.DataFrame(rowsx, columns=labels) 


#verify the connection 
print (con.version) 
#verify the connection 

#very big table# 
sql2 = """Select col1,col2,col3 from bigT""" 
#very big table# 

try: 
    cursor.execute (sql2) 
except cx_Oracle.DatabaseError: 
    print ('Failed \n'+sql2) 

#need to do it in chunk as not enough memory and blow up! 
while True: 
    rows = cursor.fetchmany() 
    if rows == []: 
     break; 
    df2=pd.DataFrame.from_records(rows,columns=labels)  
    df=df.append(df2)