Install gspread-dataframe
masterTo use this library, install it via pip. It is designed to work with gspread and pandas to facilitate moving data between DataFrames and Google Sheets.
pip install gspread-dataframerepository·master·Indexed 42 days ago
https://github.com/robin900/gspread-dataframeA 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.
To use this library, install it via pip. It is designed to work with gspread and pandas to facilitate moving data between DataFrames and Google Sheets.
pip install gspread-dataframeYou can install gspread-dataframe via PyPI using pip.
Requirements:
gspread >= 3.0.0pandas >= 0.24.0pip install gspread-dataframeIf 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)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)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)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)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)