
import pygsheets
path='G:\\My drive\\testing\\google-sheet\\creds1.json'
gc=pygsheets.authorize(service_account_file=path)
sh=gc.create('my_sheet1',folder_name='python-test')
set_dataframe(df, start, copy_index=False, copy_head=True,
extend=False, fit=False, escape_formulae=False, **kwargs)
Pandas DataFrame
| Options | Details |
|---|---|
| df | Pandas DataFrame |
| start | Staring Point tuple ( row, column). It is (1,1) in below example |
| copy_index | Default False, Copy Index data from DataFrame |
| copy_head | True ( default ), column headers of Pandas DataFrame is copied ( see example below ). |
| extend | False( default) If required add columns and rows ( see example ) |
| fit | False(default), Resize to fit all data in DataFrame if required ( see example) |
| escape_formulae | How to handle formula ( value with = or +- sign ) ( see example below ) |
| nan | Replace NaN with input value ( see example below ) |

import pygsheets
path='G:\\My drive\\testing\\google-sheet\\creds1.json'
gc=pygsheets.authorize(service_account_file=path)
sh=gc.open('my_gsheets1') # open the existing file
wk1=sh[0] # first worksheet
#wk1.clear() # to remove all data from sheet
import pandas as pd # using pandas library
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,60]
} # Create dictionary
df = pd.DataFrame(data=my_dict) # Create DataFrame using dictionary
wk1.set_dataframe(df,(1,1)) # Place DataFrame from row 1 column 1
print(wk1.get_row(4,include_tailing_empty=False)) # for testing
Output ( for last line )
['Alex', '3', '50', '60']
wk1.set_dataframe(df,(1,1),copy_index=True) #

wk1.set_dataframe(df,(1,1),copy_index=True, copy_head=False)

wk1.resize(2,2)
wk1.set_dataframe(df,(1,1),copy_index=True, extend=True)

wk1.resize(10,10) # resize rows and columns of sheet
wk1.set_dataframe(df,(1,1),copy_index=True, fit=True)

'=40+5'. Default value for escape_Formulae is False.
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30, '=40+5',50],
'ENGLISH':[20,30,60]
} # Create dictionary
df = pd.DataFrame(data=my_dict) # Create DataFrame using dictionary
wk1.set_dataframe(df,(1,1), escape_formulae=False)

wk1.set_dataframe(df,(1,1), escape_formulae=True)

import pandas as pd # using pandas library
import numpy as np
my_dict={'NAME':['Ravi','Raju','Alex',None,'King',None],
'ID':[1,2,np.NaN,4,5,6],
'MATH':[80,40,70,70,82,30],
'ENGLISH':[81,70,40,50,np.NaN,30]}
df = pd.DataFrame(data=my_dict) # Create DataFrame using dictionary
wk1.set_dataframe(df,(1,1))

wk1.set_dataframe(df,(1,1), nan='*')

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.