Wednesday, 29 March 2023

Saving a .CSV file to Google sheets from colab

I am reading from a Google sheets file in Colab, but I'd like to dump a pandas dataframe in its entirety to the colab. I've seen code snippets that deal with ranges, but I'd like some better way to "import" the entire CSV. Does anyone have the code for that?

from google.colab import auth
auth.authenticate_user()

import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
sh = gc.create('A new spreadsheet')


worksheet = gc.open('A new spreadsheet').sheet1

When reading I have the following code:

rows = worksheet.get_all_records()

What is the equivalent for dumping a dataframe i.e. via .csv

I have tried writing rows as:

worksheet = gc.open('Capital Gain Output').sheet1
worksheet.add_rows(results)

where results is a list of dictionaries (one for each row).



from Saving a .CSV file to Google sheets from colab

No comments:

Post a Comment