Created
May 7, 2021 12:28
-
-
Save VildMedPap/38f366e3b4659d65cc83f26ca6fbc2fe to your computer and use it in GitHub Desktop.
Python: Fetch data from Google Sheet
This file contains hidden or 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
import gspread | |
import pandas as pd | |
from oauth2client.service_account import ServiceAccountCredentials | |
creds = ServiceAccountCredentials.from_json_keyfile_name( | |
filename="credentials.json", | |
scopes=["https://www.googleapis.com/auth/drive"] | |
) | |
# Initialize client | |
client = gspread.authorize(creds) | |
# Fetch name of first spreadsheet file | |
spreadsheet_file = client.list_spreadsheet_files()[0]["name"] | |
# Open spreadsheet | |
sh = client.open(spreadsheet_file) | |
# Get all values from first worksheet | |
data = sh.worksheets()[0].get_all_values() | |
# Use first row as column names and all subsequent rows as data | |
df = pd.DataFrame(data[1:], columns=data[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment