Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Google Sheets on Google Colaboratory

Hi I am using Google Colaboratory (similar to Jupyter Notebook). Does anyone know how to access data from Google Sheets using Google Colaboratory notebook?

like image 355
AI2.0 Avatar asked Jan 26 '18 22:01

AI2.0


People also ask

How do I upload a spreadsheet to google Colab?

Manual Method 1 — using files. upload() to upload data to Colab. Using files. upload() directly in the Colab notebook gives you a traditional upload button that allows you to move files from your computer into to the Colab environment.

How do I connect files to google Colab?

Click on “Choose Files” then select and upload the file. Wait for the file to be 100% uploaded. You should see the name of the file once Colab has uploaded it. Finally, type in the following code to import it into a dataframe (make sure the filename matches the name of the uploaded file).


2 Answers

Loading data from Google Sheets is covered in the I/O example notebook: https://colab.research.google.com/notebook#fileId=/v2/external/notebooks/io.ipynb&scrollTo=sOm9PFrT8mGG

like image 164
Bob Smith Avatar answered Oct 10 '22 07:10

Bob Smith


!pip install --upgrade -q gspread


import gspread
import pandas as pd
from google.colab import auth
auth.authenticate_user()
from google.auth import default

creds, _ = default()
gc = gspread.authorize(creds)


worksheet = gc.open('data_set.csv').sheet1
rows = worksheet.get_all_values()
pd.DataFrame.from_records(rows)
like image 34
Alon Lavian Avatar answered Oct 10 '22 07:10

Alon Lavian