Difference between revisions of "Gspread"
Jump to navigation
Jump to search
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | <code>gspread</code> is an API that allows python to read and write from/to a Google sheet | + | <code>gspread</code> is an API that allows python to read and write from/to a Google sheet ([https://github.com/burnash/gspread github], [https://gspread.readthedocs.io/en/latest/ readthedocs]) |
| − | |||
| − | |||
* <code>gspread-dataframe</code> helps deal with pandas objects ([https://pypi.org/project/gspread-dataframe/ pypi], [https://github.com/robin900/gspread-dataframe github]) | * <code>gspread-dataframe</code> helps deal with pandas objects ([https://pypi.org/project/gspread-dataframe/ pypi], [https://github.com/robin900/gspread-dataframe github]) | ||
* Google's [https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#resource:-spreadsheet REST API v4] documentation | * Google's [https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#resource:-spreadsheet REST API v4] documentation | ||
| − | To write a bunch of dataframes also requires the gspread-dataframe package's <code>set_with_dataframe</code> functionality | + | To write a bunch of dataframes also requires the gspread-dataframe package's <code>set_with_dataframe</code> functionality: |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
import gspread | import gspread | ||
from oauth2client.service_account import ServiceAccountCredentials | from oauth2client.service_account import ServiceAccountCredentials | ||
| − | gc = gspread.oauth() # this may open a new browser | + | gc = gspread.oauth() # this may open a new browser to authenticate your google acct |
sh = gc.open('name_of_workbook') | sh = gc.open('name_of_workbook') | ||
worksheet = sh.get_worksheet(0) #0 for the first sheet | worksheet = sh.get_worksheet(0) #0 for the first sheet | ||
| Line 18: | Line 16: | ||
ranked.rename(columns={'index': names[i], 'product':'Count'}, inplace=True) | 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 | set_with_dataframe(worksheet, ranked, row=1, col=(i+1)*4 -3) #stagger frames by 4 cols | ||
| + | </syntaxhighlight> | ||
Latest revision as of 21:25, 4 February 2021
gspread is an API that allows python to read and write from/to a Google sheet (github, readthedocs)
gspread-dataframehelps deal with pandas objects (pypi, github)- Google's REST API v4 documentation
To write a bunch of dataframes also requires the gspread-dataframe package's set_with_dataframe functionality:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
gc = gspread.oauth() # this may open a new browser to authenticate your google acct
sh = gc.open('name_of_workbook')
worksheet = sh.get_worksheet(0) #0 for the first sheet
# 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