Simplify your Data Management with gsheet-pandas 0.2.5

Video pandas dataframe to google sheet

Latest PyPI Version License

Are you tired of all the hassle when working with Google Sheets? Introducing gsheet-pandas, a package that simplifies data management by allowing you to easily obtain a Pandas dataframe from Google Sheets or upload a dataframe to Sheets.

Easy Installation

Installing gsheet-pandas is a breeze. Just use the following pip command:

pip install gsheet-pandas

Configuring Your Environment

Activating the API

Before you can start using the Google APIs, you need to activate them in a Google Cloud project. Follow these simple steps:

  1. Go to Menu > API and Services > Credentials in the Google Cloud Console.
  2. Navigate to Credentials.
  3. Click on Create Credentials > OAuth Client ID.
  4. Select Application Type > Desktop Application.
  5. Fill in the Name field with a name for the identifier. This name will only appear in the Google Cloud Console.
  6. Click Create. The OAuth client creation screen will appear, showing your new client ID and client secret.
  7. Click OK. The newly created identity will appear under OAuth client IDs.
  8. Save the downloaded JSON file as credentials.json and move it to your working directory.

Authorizing Credentials for a Desktop Application

To authenticate as an end user and access user data in your application, you need to create one or more OAuth 2.0 client IDs. Each client ID is used to identify a single application to Google’s authentication servers. Follow these steps:

  1. In the Google Cloud Console, go to Menu > API and Services > Credentials.
  2. Navigate to Credentials.
  3. Click on Create Credentials > OAuth Client ID.
  4. Select Application Type > Desktop Application.
  5. Fill in the Name field with a name for the credentials. This name will only appear in the Google Cloud Console.
  6. Click Create. The OAuth client creation screen will appear, showing your new client ID and client secret.
  7. Click OK. The newly created credentials will appear under OAuth client IDs.
  8. Save the downloaded JSON file as credentials.json and move it to your working directory.

How to Use gsheet-pandas

Pandas Extension

To get started, call the configuration method to save your credentials and initialize the Pandas extensions:

from pathlib import Path
import gsheet_pandas

secret_path = Path('/path/to/your/secrets/').resolve()
gsheet_pandas.setup(
    credentials_dir=secret_path / 'credentials.json',
    token_dir=secret_path / 'token.json'
)

To download a dataframe from Google Sheets:

import pandas as pd

df = pd.from_gsheet(drive_table=table_name, sheet_name=sheet_name, range_name='!A1:C100') # Range in Sheets (optional)

The default range is !A1:ZZ900000.

To upload a dataframe to Google Sheets:

df.to_gsheet(
    drive_table=table_name,
    sheet_name=sheet_name,
    range_name='!B1:ZZ900000', # Range in Sheets (optional)
    drop_columns=False
) # Upload column names or not (optional)

DriveConnection Instance

To get started, initialize a DriveConnection instance:

from gsheet_pandas import DriveConnection

secret_path = Path('/path/to/your/secrets/').resolve()
drive = DriveConnection(
    credentials_dir=secret_path / 'credentials.json',
    token_dir=secret_path / 'token.json'
)

To download a dataframe from Google Sheets:

df = drive.download(
    drive_table=table_name,
    sheet_name=sheet_name,
    range_name='!A1:C100', # Range in Sheets (optional)
    header=0
)

The default range is !A1:ZZ900000.

To upload a dataframe to Google Sheets:

df = drive.upload(
    df,
    drive_table=table_name,
    sheet_name=sheet_name,
    range_name='!B1:ZZ900000', # Range in Sheets (optional)
    drop_columns=False
) # Upload column names or not (optional)

Discover more features and simplify your data management tasks with Google Sheets using gsheet-pandas. Visit Crawlan.com to learn more!

Original article: gsheet-pandas 0.2.5

Related posts