Last active
March 29, 2024 23:33
-
-
Save korakot/3c9a33c12c6547ed38b2ca4b1e311426 to your computer and use it in GitHub Desktop.
Save dataframe to Google Sheet from Colab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# authenticate | |
from google.colab import auth | |
auth.authenticate_user() | |
import gspread | |
from oauth2client.client import GoogleCredentials as GC | |
gc = gspread.authorize(GC.get_application_default()) | |
# create, and save df | |
from gspread_dataframe import set_with_dataframe | |
title = 'New Sheet' | |
gc.create(title) # if not exist | |
sheet = gc.open(title).sheet1 | |
set_with_dataframe(sheet, df) | |
# include_index=False, include_column_header=True, resize=False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gspread_dataframe import get_as_dataframe | |
sheet = gc.open(title).sheet1 | |
df = get_as_dataframe(sheet).dropna(0, 'all').dropna(1, 'all') | |
# remove empty (NA) columns and rows |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gspread | |
from gspread_dataframe import * | |
def set_to(self, df): | |
set_with_dataframe(self, df) | |
gspread.models.Worksheet.set_to = set_to | |
def get_df(self): | |
return get_as_dataframe(self).dropna(0, 'all').dropna(1, 'all') | |
gspread.models.Worksheet.get_df = get_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment