Created
March 8, 2022 21:05
-
-
Save JohnLockwood/e3e114d6a1d85ef572b44201f7343cfa to your computer and use it in GitHub Desktop.
gspread_dataframe demo for CodeSolid article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "eb7b2ce8-a1fa-44be-ac4d-5693020e0169", | |
"metadata": {}, | |
"source": [ | |
"# Demonstration of gspread_dataframe\n", | |
"\n", | |
"This is a companion notebook to the article, [How to Work With Google Sheets in Python and Pandas](https://codesolid.com/google-sheets-in-python-and-pandas/). This notebook shows how to tie it all together into Pandas.\n", | |
"\n", | |
"See that article for setup and configuration information you'll need." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"id": "62cd041f-c59f-4a0d-a8c7-e16491211c95", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>0</th>\n", | |
" <th>1</th>\n", | |
" <th>2</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>8</td>\n", | |
" <td>9</td>\n", | |
" <td>10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>16</td>\n", | |
" <td>17</td>\n", | |
" <td>18</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>24</td>\n", | |
" <td>25</td>\n", | |
" <td>26</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>32</td>\n", | |
" <td>33</td>\n", | |
" <td>34</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>40</td>\n", | |
" <td>41</td>\n", | |
" <td>42</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>48</td>\n", | |
" <td>49</td>\n", | |
" <td>50</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>56</td>\n", | |
" <td>57</td>\n", | |
" <td>58</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" 0 1 2\n", | |
"0 0 1 2\n", | |
"1 8 9 10\n", | |
"2 16 17 18\n", | |
"3 24 25 26\n", | |
"4 32 33 34\n", | |
"5 40 41 42\n", | |
"6 48 49 50\n", | |
"7 56 57 58" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"\n", | |
"import gspread\n", | |
"from gspread_dataframe import get_as_dataframe, set_with_dataframe\n", | |
"\n", | |
"# Open an existing spreadsheet\n", | |
"gc = gspread.service_account()\n", | |
"sh = gc.open(\"AnalyticsSnapshot\")\n", | |
"\n", | |
"# Read a worksheet and create it if it doesn't exist\n", | |
"worksheet_title = \"Pandas Demo\"\n", | |
"try:\n", | |
" worksheet = sh.worksheet(worksheet_title)\n", | |
"except gspread.WorksheetNotFound:\n", | |
" worksheet = sh.add_worksheet(title=worksheet_title, rows=1000, cols=1000)\n", | |
"\n", | |
"# Write a test DataFrame to the worksheet\n", | |
"df_test = pd.DataFrame(np.arange(64).reshape(8,8))\n", | |
"set_with_dataframe(worksheet, df_test)\n", | |
"\n", | |
"# Get some columns back out\n", | |
"df_read = get_as_dataframe(worksheet, usecols=[0,1,2], nrows=8, header=None, skiprows=1)\n", | |
"\n", | |
"# Display the columns\n", | |
"df_read\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment