How do i read a google spreadsheet in pandas?

How to read, update, and create a Google spreadsheet and build a DataFrame pandas.

How do i read a google spreadsheet in pandas?

Index

  1. Create a project in Google Developers.
  2. Active API Google Sheets and Google Drive.
  3. Keys for authenticating: Local or Service
  4. Create Spreadsheet.
  5. Script Python + Pandas.

How do i read a google spreadsheet in pandas?

Create a project in Google Developers

We need to create a project in order to use the Google Sheets API resources.

Nothing works without that project.

Link: https://console.developers.google.com/project

  1. Create a project.

How do i read a google spreadsheet in pandas?

2. Set a name for the project.

How do i read a google spreadsheet in pandas?

How do i read a google spreadsheet in pandas?

Google Sheets API

Now we start API to interchange sheets with the project, then a type of API is chosen: public or a private one.

  1. In the search bar find the "Google Sheets API".

How do i read a google spreadsheet in pandas?

2. Set an "Enable" option.

How do i read a google spreadsheet in pandas?

3. Come back to the main page and choose "OAuth consent screen".

How do i read a google spreadsheet in pandas?

4. We need to define what type of API is chosen; the public one allows any person to access your API, the private one is accessed only by the user who has the code.

In this example, the public API has been chosen.

How do i read a google spreadsheet in pandas?

5. After selecting a type, a name for the API has to be set.
There are other options; for this example it is not necessary to set it.

How do i read a google spreadsheet in pandas?

6. Enable the Google Drive API.

How do i read a google spreadsheet in pandas?

It is necessary to create and update spreadsheets.

Up to now we have:

* Created a project in Google Developers.

* Activated API Sheets and defined a type of API.

We have created the base of the project.

How do i read a google spreadsheet in pandas?

Keys

Authenticating — We need to generate keys to access the spreadsheets. There are two kinds of keys.

1 — OAuth client ID:

Requires the user’s consent so the app can only access the user’s data.

Recommended for authenticating and developing a local application.

2 — Service account:

Enables a server-to-server function, that is an app-level authentication using the robot accounts.

Recommended for automatic authentication, used generally in production applications (authentication is no longer local). We have created a specific access key for these cases.

Creating a local key

  1. In Credentials, choose "+ Create Credentials" and OAuth client Id.

How do i read a google spreadsheet in pandas?

2. Select an option of the device that will be authenticated, in our case: Desktop app.

How do i read a google spreadsheet in pandas?

How do i read a google spreadsheet in pandas?

Generated key access tokens.

3. Download the key.

How do i read a google spreadsheet in pandas?

.json file

Creating a remote key

  1. In Credentials choose “+ Create Credentials” and service account.

How do i read a google spreadsheet in pandas?

2. Set a name for your key.

How do i read a google spreadsheet in pandas?

The rest of the steps are not compulsory, so we can miss it out.

3. Successfully created key.

How do i read a google spreadsheet in pandas?

Now to generate .json file, click on the name of the key, and we reach the Keys section.

  1. Make sure to use the right key you have selected and create a new key.

How do i read a google spreadsheet in pandas?

2. Choose the type of file, .json.

How do i read a google spreadsheet in pandas?

3. The key will be saved on your computer.

How do i read a google spreadsheet in pandas?

4. Save the generated e-mail in the key, so that it can be accessed in the spreadsheet.

How do i read a google spreadsheet in pandas?

How do i read a google spreadsheet in pandas?

Sheets

There are no mistery here — we have created spreadsheets so we need to pay attention to two points:

To the Spreadsheet key:
This key identifies the code, it is necessary for the operation of the code. This function is necessary to identify the operation of the code.

How do i read a google spreadsheet in pandas?

Spreadsheet key

Access the remote email key:
You must grant access to the email key by using the remote key.

How do i read a google spreadsheet in pandas?

How do i read a google spreadsheet in pandas?

Python

To create a Python code, you can follow these three examples to realize how the whole process works. Be sure to have the authentication keys in your project folder.

To execute these examples, it is necessary to install the following libs:

!pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib!pip install gspread!pip install pandas

1st example — Read the spreadsheet local key.
Based in google code, we adapted to return one the pandas dataframe.
For more details, see the Google code.

For the first time authentification, the Google service will create a token.pickle file, which will not be required the second time.

2nd example — Read the spreadsheet remote key.
This example needs a service key. The reading function is rewritten just to facilitate the authentication and the understanding of the process.

From this point on the token pickle file will no longer be required.
See how the code is easily recognized.

3rd example — Read, updated and create spreadsheet (Service key).

By now we can already understand how the authentication models (location and service) work.

In this script, the code is abstracted in four functions:
Authentication — Authentification can be created with APIs.
Reading — A specific spreadsheet can be read.
Update — A specific tab has been updated. It is necessary that this tab is already in use.
Creates and inserts the data — Anew spreadsheet has been created and the DataFrame inserted. It is important to share an email which will access the new spreadsheet. As a service email is authenticated, the email must not be shared. This is the way to access the spreadsheet.

Thanks for reading this article.

The author is available to receive feedbacks.

Follow him on LikedIn or Github.

How do I read a Google Sheet in Python?

Let's look at the setup we need to make step by step..
Create a project – Let us create a project in google drive. ... .
Enable the APIs – Now we will enable two APIs – Google drive API and Google Sheets API. ... .
Create Credentials – Click on create credentials and create a Json Key..

How do I read a Google Sheet in Jupyter notebook?

Connecting to the Google Sheet Click on the share button at the top right corner of the Google sheet, paste this address and hit SEND. We are now ready to connect to this sheet. Open up a Jupyter notebook. We will use a Python library called gspread, which can be installed with pip.

How do I connect Google Sheets to Python?

Search for 'Google Drive API', enable it. Select Compute Engine service default, JSON, hit create. Open up the JSON file, share your spreadsheet with the "[email protected]" email listed. Save the JSON file wherever you're hosting your project, you'll need to load it in through Python later.

How do I read data in Google Sheets?

Get data from other sheets in your spreadsheet.
On your computer, go to docs.google.com/spreadsheets/..
Open or create a sheet..
Select a cell..
Type = followed by the sheet name, an exclamation point, and the cell being copied. For example, =Sheet1! A1 or ='Sheet number two'! B4 ..