Skip to content

Instantly share code, notes, and snippets.

@Dminor7
Last active October 17, 2020 23:13
Show Gist options
  • Save Dminor7/0b0cb8d6b711a3bedd72a14f312883d1 to your computer and use it in GitHub Desktop.
Save Dminor7/0b0cb8d6b711a3bedd72a14f312883d1 to your computer and use it in GitHub Desktop.
Python pandas dataframe to google sheets [Read, Write, and Append]
import gspread_dataframe as gd
import gspread as gs
gc = gs.service_account(filename="credentials.json")
def export_to_sheets(sheet_name,df,mode='r'):
ws = gc.open("Hashnode").worksheet(sheet_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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment