Case: My script returns a data frame that needs has to be appended to an existing google spreadsheet as new rows of data.As of now, I'm appending a data frame as multiple single rows through gspread.
My Code:
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)
Is there a way to append entire data-frame rather than multiple single rows?
Search for 'Google Drive API', enable it. Select Compute Engine service default, JSON, hit create. Open up the JSON file, share your spreadsheet with the "[email protected]" email listed. Save the JSON file wherever you're hosting your project, you'll need to load it in through Python later.
append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.
I can recommend 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)
Here is the code to write, append(without loading the existing sheet into memory), and read to google sheets.
import gspread_dataframe as gd
import gspread as gs
gc = gs.service_account(filename="your/cred/file.json")
def export_to_sheets(worksheet_name,df,mode='r'):
ws = gc.open("SHEET_NAME").worksheet("worksheet_name")
if(mode=='w'):
ws.clear()
gd.set_with_dataframe(worksheet=ws,dataframe=df,include_index=False,include_column_header=True,resize=True)
return True
elif(mode=='a'):
ws.add_rows(df.shape[0])
gd.set_with_dataframe(worksheet=ws,dataframe=df,include_index=False,include_column_header=False,row=ws.row_count+1,resize=False)
return True
else:
return gd.get_as_dataframe(worksheet=ws)
df = pd.DataFrame.from_records([{'a': i, 'b': i * 2} for i in range(100)])
export_to_sheets("SHEET_NAME",df,'a')
ws.clear()
.Second using set_with_dataframe()
uploading the dataframe,
here note that resize=True
, which strictily set the row and col in worksheet to df.shape. This will help later in append method.resize=False
as we are adding rows and row=ws.row_count+1
anchoring its row value for append.I was facing the same problem, here's what I did
converted the dataframe into list and used gspread's append_rows()
gc = gspread.service_account(filename="credentials.json")
sh = gc.open_by_key('<your_key>')
ws = sh.sheet1
##data is the original data frame
data_list = data.values.tolist()
ws.append_rows(data_list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With