gspread-dataframe

repository·master·Indexed 42 days ago

https://github.com/robin900/gspread-dataframe

A Python library to read and write Google Sheets worksheets using pandas DataFrames. It provides the get_as_dataframe function to ingest spreadsheet data and the set_with_dataframe function to export DataFrames to Google Sheets, requiring gspread >= 3.0.0 and pandas >= 0.24.0.

Tokens
1.3K
Snippets
7
Records
7
Agent score
4%

What's inside gspread-dataframe

  1. Format a Google worksheet using gspread-formatting

    master

    If you install the gspread-formatting package, you can use format_with_dataframe to apply formatting to the Google worksheet to match the data you just wrote. This is useful for styling headers and cell ranges.

    import pandas as pd
    from gspread_dataframe import get_as_dataframe, set_with_dataframe
    from gspread_formatting.dataframe import format_with_dataframe
    
    worksheet = some_worksheet_obtained_from_gspread_client
    
    df = pd.DataFrame.from_records([{'a': i, 'b': i * 2} for i in range(100)])
    set_with_dataframe(worksheet, df)
    
    # Apply formatting to the worksheet based on the DataFrame
    format_with_dataframe(worksheet, df, include_column_header=True)
  2. Write a Pandas DataFrame to a Google worksheet with set_with_dataframe

    master

    Use set_with_dataframe(worksheet, df) to write a Pandas DataFrame to a Google worksheet. The worksheet object must be an instance obtained via the gspread package.

    import pandas as pd
    from gspread_dataframe import set_with_dataframe
    
    # worksheet must be obtained from a gspread client
    worksheet = some_worksheet_obtained_from_gspread_client
    
    df = pd.DataFrame.from_records([{'a': i, 'b': i * 2} for i in range(100)])
    set_with_dataframe(worksheet, df)
  3. Read a Google worksheet as a DataFrame with get_as_dataframe

    master

    Use get_as_dataframe(worksheet, ...) to retrieve a Google worksheet as a Pandas DataFrame.

    Parsing Options: This function supports keyword arguments compatible with the Pandas 'python' engine used in pandas.read_csv. Examples include parse_dates, usecols, skiprows, and header.

    New in v4.0.0:

    • drop_empty_rows: (bool, default True) Automatically discards empty rows from the returned DataFrame.
    • drop_empty_columns: (bool, default True) Automatically discards empty columns from the returned DataFrame.

    These parameters are useful when a worksheet has a large default size (e.g., 1000 rows, 26 columns) but only contains data in a specific range.

    import pandas as pd
    from gspread_dataframe import get_as_dataframe
    
    worksheet = some_worksheet_obtained_from_gspread_client
    
    # Example with parsing options
    df = get_as_dataframe(worksheet, parse_dates=True, usecols=[0,2], skiprows=1, header=None)
    
    # Example using v4.0.0 empty row/column dropping
    df = get_as_dataframe(worksheet, drop_empty_rows=True, drop_empty_columns=True)
  4. Use get_as_dataframe to read a Google Sheet into a Pandas DataFrame

    master

    The get_as_dataframe function allows you to pull data from a Google Sheet (via a gspread worksheet object) directly into a pandas DataFrame. This is useful for ingesting spreadsheet data for analysis.

    import gspread
    from gspread_dataframe import get_as_dataframe
    
    # Assuming 'worksheet' is an existing gspread worksheet object
    df = get_as_dataframe(worksheet)
  5. Use set_with_dataframe to write a Pandas DataFrame to a Google Sheet

    master

    The set_with_dataframe function allows you to write an entire pandas DataFrame into a Google Sheet (via a gspread worksheet object). This is useful for exporting processed data back to a spreadsheet.

    import gspread
    import pandas as pd
    from gspread_dataframe import set_with_dataframe
    
    # Assuming 'worksheet' is an existing gspread worksheet object
    # and 'df' is your pandas DataFrame
    set_with_dataframe(worksheet, df)