A little outdated answer, but I hope it might help others.
I wrote my own convenient package to connect Google Sheets and pandas dataframes. One can install package using pip:
pip install gsheet-pandas
The short answer is:
from gsheet_pandas import DriveConnection
drive = DriveConnection(credentials_dir='credentials.json')
drive.upload(df, drive_table=table_name, sheet_name=sheet_name)
Full answer: library provides two ways to work with pandas.
The first one is pandas extension:
import gsheet_pandas
# To login with a service account
gsheet_pandas.setup(credentials_dir='service_creds.json')
# To login with a user account
gsheet_pandas.setup(credentials_dir='creds.json', token_dir='token.json')
# Uploading df
df.to_gsheet(drive_table=table_name, sheet_name=sheet_name)
# Fetching df
df = pd.from_gsheet(drive_table=table_name, sheet_name=sheet_name)
Second option is to use DriveConnection class:
from gsheet_pandas import DriveConnection
# Init DriveConnection instance
drive = DriveConnection(credentials_dir='credentials.json')
# Uploading dataframe
drive.upload(df, drive_table=table_name,
sheet_name=sheet_name,
range_name='!B1:ZZ900000', # Range in Sheets; Optional
drop_columns=False) # Upload column names or not; Optional
# Fetching dataframe
df = drive.download(drive_table=table_name,
sheet_name=sheet_name,
range_name='!A1:C100', # Range in Sheets; Optional
header=0) # Column row
worksheet.clear()to clear the contents then apply thisworksheet.update([dataframe.columns.values.tolist()] + dataframe.values.tolist())or the one you posted as a solution to fully update whatever is yourdataframevalue but may I ask is there a way to not to a.clear()? for like update as it what is the value of the prefered dataframe to be uploaded?# Initialize Google Sheets google_sheet_id = Sheet_ID google_sheet_name = 'Sheet_Name' URL = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format( google_sheet_id, google_sheet_name ) df = pd.read_csv(URL)