Read/write Google spreadsheets using pandas DataFrames
gspread-dataframe
.. image:: https://badge.fury.io/py/gspread-dataframe.svg :target: https://badge.fury.io/py/gspread-dataframe
.. image:: https://github.com/robin900/gspread-dataframe/actions/workflows/python-package.yml/badge.svg?branch=master :target: https://github.com/robin900/gspread-dataframe/actions/workflows/python-package.yml
.. image:: https://img.shields.io/pypi/dm/gspread-dataframe.svg :target: https://pypi.org/project/gspread-dataframe
This package allows easy data flow between a worksheet in a Google spreadsheet and a Pandas DataFrame. Any worksheet you can obtain using the `gspread package can be retrieved as a DataFrame with getasdataframe; DataFrame objects can be written to a worksheet using setwithdataframe:
.. code:: python
import pandas as pd from gspreaddataframe import getasdataframe, setwith_dataframe
worksheet = someworksheetobtainedfromgspread_client
df = pd.DataFrame.from_records([{'a': i, 'b': i * 2} for i in range(100)]) setwithdataframe(worksheet, df)
df2 = getasdataframe(worksheet)
The getasdataframe function supports the keyword arguments that are supported by your Pandas version's text parsing readers, such as pandas.readcsv. Consult your Pandas documentation for a full list of options __. Since the 'python' engine in Pandas is used for parsing, only options supported by that engine are acceptable:
.. code:: python
import pandas as pd from gspreaddataframe import getas_dataframe
worksheet = someworksheetobtainedfromgspread_client
df = getasdataframe(worksheet, parse_dates=True, usecols=[0,2], skiprows=1, header=None)
New in version 4.0.0: dropemptyrows and dropemptycolumns parameters, both True by default, are now accepted by getasdataframe. If you created a Google sheet with the default number of columns and rows (26 columns, 1000 rows), but have meaningful values for the DataFrame only in the top left corner of the worksheet, these parameters will cause any empty rows or columns to be discarded automatically and absent from the returned DataFrame.
Formatting Google worksheets for DataFrames ~~~~~~~~~~~
If you install the gspread-formatting package, you can additionally format a Google worksheet to suit the DataFrame data you've just written. See the package documentation for details
.. code:: python
import pandas as pd from gspreaddataframe import getasdataframe, setwith_dataframe from gspreadformatting.dataframe import formatwith_dataframe
worksheet = someworksheetobtainedfromgspread_client
df = pd.DataFrame.from_records([{'a': i, 'b': i * 2} for i in range(100)]) setwithdataframe(worksheet, df) formatwithdataframe(worksheet, df, includecolumnheader=True)
Installation
Requirements ~~~~
- Python 3 only, for releases 4.0.0 and later
- Python 2.7 and 3 for releases prior to 4.0.0
- gspread (>=3.0.0; to use older versions of gspread, use gspread-dataframe releases of 2.1.1 or earlier)
- Pandas >= 0.24.0
.. code:: sh
pip install gspread-dataframe
From GitHub ~~~
.. code:: sh
git clone https://github.com/robin900/gspread-dataframe.git cd gspread-dataframe python setup.py install