2017-08-07 88 views
2

案例: 我的腳本返回一個數據框,需要將其作爲新的數據行添加到現有的Google電子表格中。截至目前,我將數據框附加爲多個單一行通過gspread。將pandas數據框附加到Google電子表格中

我的代碼:

import gspread 
    import pandas as pd 
    DF=pd.DataFrame() 

    #After some processing a non-empty data frame has been created. 

    Output_conn = gc.open("SheetName").worksheet("xyz") 

    # Here 'SheetName' is google spreadsheet and 'xyz' is sheet in the workbook 

    for i,row in DF.iterrows(): 
     Output_conn.append_row(Row) 

是否有辦法追加整個數據幀而不是多個單行?

回答

1

我可以推薦gspread-dataframe

import gspread_dataframe as gd 

# Connecting with `gspread` here 

ws = gc.open("SheetName").worksheet("xyz") 
existing = gd.get_as_dataframe(ws) 
updated = existing.append(your_new_data) 
gd.set_with_dataframe(ws, updated) 
0

如果谷歌電子表格需要.csv格式,那麼你可以將一個熊貓數據框使用df.to_csv()到CSV和其保存在該格式

相關問題