Gspread

From Bitnami MediaWiki
Revision as of 21:22, 4 February 2021 by User (talk | contribs)
Jump to navigation Jump to search

gspread is an API that allows python to read and write from/to a Google sheet.

To write a bunch of dataframes also requires the gspread-dataframe package's set_with_dataframe functionality. <syntaxhighlight lang=python> import gspread from oauth2client.service_account import ServiceAccountCredentials gc = gspread.oauth() # this may open a new browser sh = gc.open('name_of_workbook') worksheet = sh.get_worksheet(0) #0 for the first sheet

  1. Now for the df part

my_dfs = [a, b, c, d, e] for i, df in enumerate(my_dfs):

 ranked = pd.DataFrame(df['product'].value_counts().reset_index()
 ranked.rename(columns={'index': names[i], 'product':'Count'}, inplace=True)
 set_with_dataframe(worksheet, ranked, row=1, col=(i+1)*4 -3)  #stagger frames by 4 cols