Last active
March 30, 2024 03:13
-
-
Save lucascheung/ec670b961c35a9f7bcb2414beee34510 to your computer and use it in GitHub Desktop.
Upload pandas dataframe Google Sheet
This file contains hidden or 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 json | |
from datetime import datetime | |
import gspread | |
from oauth2client.service_account import ServiceAccountCredentials | |
from df2gspread import df2gspread as d2g | |
def upload_to_sheet(df,sheet_id, sheet_name): | |
scope = ['https://spreadsheets.google.com/feeds', | |
'https://www.googleapis.com/auth/drive'] | |
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope) | |
spreadsheet_key = sheet_id | |
wks_name = sheet_name | |
if not df.empty: | |
d2g.upload(df, spreadsheet_key, wks_name, credentials=credentials, row_names=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment