Examples

Google Spreadsheet to Pandas DataFrame

df2gspread.gspread2df.download(gfile, wks_name=None, col_names=False, row_names=False, credentials=None, start_cell='A1')

Download Google Spreadsheet and convert it to Pandas DataFrame

Parameters:
  • gfile (str) – path to Google Spreadsheet or gspread ID
  • wks_name (str) – worksheet name
  • col_names (bool) – assing top row to column names for Pandas DataFrame
  • row_names (bool) – assing left column to row names for Pandas DataFrame
  • credentials (class 'oauth2client.client.OAuth2Credentials') – provide own credentials
  • start_cell (str) – specify where to start capturing of the DataFrame; default is A1
Returns:

Pandas DataFrame

Return type:

class ‘pandas.core.frame.DataFrame’

Example:
>>> from df2gspread import gspread2df as g2d
>>> df = g2d.download(gfile="1U-kSDyeD-...", col_names=True, row_names=True)
>>> df
       col1 col2
field1    1    2
field2    3    4

Pandas DataFrame to Google Spreadsheet

df2gspread.df2gspread.upload(df, gfile='/New Spreadsheet', wks_name=None, chunk_size=1000, col_names=True, row_names=True, clean=True, credentials=None, start_cell='A1', df_size=False, new_sheet_dimensions=(1000, 100))

Upload given Pandas DataFrame to Google Drive and returns gspread Worksheet object

Parameters:
  • df (class 'pandas.core.frame.DataFrame') – Pandas DataFrame
  • gfile (str) – path to Google Spreadsheet or gspread ID
  • wks_name (str) – worksheet name
  • chunk_size (int) – size of chunk to upload
  • col_names (bool) – passing top row to column names for Pandas DataFrame
  • row_names (bool) – passing left column to row names for Pandas DataFrame
  • clean (bool) – clean all data in worksheet before uploading
  • credentials (class 'oauth2client.client.OAuth2Credentials') – provide own credentials
  • start_cell (str) – specify where to insert the DataFrame; default is A1
  • df_size (bool) – -If True and worksheet name does NOT exist, will create a new worksheet that is the size of the df; otherwise, by default, creates sheet of 1000x100 cells. -If True and worksheet does exist, will resize larger or smaller to fit new dataframe. -If False and dataframe is larger than existing sheet, will resize the sheet larger. -If False and dataframe is smaller than existing sheet, does not resize.
  • new_sheet_dimensions (tuple) – tuple of (row, cols) for size of a new sheet
Returns:

gspread Worksheet

Return type:

class ‘gspread.models.Worksheet’

Example:
>>> from df2gspread import df2gspread as d2g
>>> import pandas as pd
>>> df = pd.DataFrame([1 2 3])
>>> wks = d2g.upload(df, wks_name='Example worksheet')
>>> wks.title
'Example worksheet'