Last active
October 17, 2020 23:13
-
-
Save Dminor7/0b0cb8d6b711a3bedd72a14f312883d1 to your computer and use it in GitHub Desktop.
Python pandas dataframe to google sheets [Read, Write, and Append]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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